TAKE OUT YEAR FROM INPUT

Mayank Tripathi

Total Post:397

Points:3117
Posted by  Mayank Tripathi
Java  String 
 664  View(s)
Ratings:
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");
}
  1. David Miller

    Post:30

    Points:210
    Re: Take out year from input

    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)));
        }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!