Home > DeveloperSection > Forums > Take out year from input
Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    December-20-2015 4:14 AM

 Java Java  String 
Ratings:


 1 Reply(s)
 313  View(s)
Rate this:
My implementation : 
public class NewClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Input your place and date birth : ");
        String birth = in.nextLine();
    }
}
And I will input MindStick, 25 Dec 2015. The problem how to change my input from string to int, and how to take only the year. So I can use for
if (2015 - year < 20 ) {
    System.out.println ("You are teen");
}


David Miller

Total Post:30

Points:210
Posted on    December-20-2015 4:17 AM

Hi Mayank ,

It would be better if you'll split the "input" into two. and then tell the user to enter his/her birthday in this format (Month Day, Year). The comma is important because after the comma the year follows.

int year = 0;
String place = "";
Scanner place_in = new Scanner(System.in);
System.out.print("Enter place of Birth : ");
place = place_in.nextLine();
String bday = "";
Scanner bday_in = new Scanner(System.in);
System.out.print("Enter Birthday (Month Day,Year): ");
bday = bday_in.nextLine();
for(int i=0;i<bday.length();i++) {
    if(bday.charAt(i)==','){
      year = Integer.parseInt(Character.toString(bday.charAt(i+2))+Character.toString(bday.charAt(i+3))+Character.toString(bday.charAt(i+4))+Character.toString(bday.charAt(i+5)));
    }
}

Don't want to miss updates? Please click the below button!

Follow MindStick