Take out year from input

Total Post:397

Java  String 
 893  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");
  1. Post:30

    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++) {
          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)));



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