javamethodsedx

BJP4 Exercise 3.20: inputBirthday


Write a method called inputBirthday that accepts a Scanner for the console as a parameter and prompts the user to enter a month, day, and year of birth, then prints the birthdate in a suitable format. Here is an example dialogue with the user:

I need to take this input like this-

On what day of the month were you born? 8
What is the name of the month in which you were born? May
During what year were you born? 1981

output should be like this-

You were born on May 8, 1981. You're mighty old!

i'm getting this kind of error

but this code works fine in intellij

public static void main(String[] args) {

    inputBirthday();
}


  public static void inputBirthday() {
      Scanner abc = new Scanner(System.in);
      System.out.println("On what day of the month were you born? ");
      int inputDay = abc.nextInt();
      System.out.println("What is the name of the month in which you were born? ");
      String inputMonth = abc.next();
      System.out.println("During what year were you born? ");
      int inputYear = abc.nextInt();
      System.out.println("You were born on " + inputMonth + " " + inputDay + "," + " " + inputYear + "." + " You're mighty old!");
}

Solution

  • public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        // either instantiate the enclosing class, or make inputBirthday static
        inputBirthday(in);
        }
    
      public static void inputBirthday(Scanner abc)
     {
        System.out.print("On what day of the month were you born? ");
        int inputDay = abc.nextInt();
        System.out.print("What is the name of the month in which you were born? ");
        String inputMonth = abc.next();
        System.out.print("During what year were you born? ");
        int inputYear = abc.nextInt();
        System.out.println("You were born on " + inputMonth + " " + inputDay + "," + " " + inputYear + "." + " You're mighty old!");
        }
    

    finally it worked and code passed all tests

    test results