-5

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:

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
You were born on May 8, 1981. You're mighty old!

So this is what I have done:

import java.util.Scanner;

public static void inputBirthday(int month,String day,int year){
    Scanner sc=new Scanner(System.in);
    System.out.print("On what day of the month were you born?");
    month=sc.nextInt();

    System.out.print("What is the name of the month in which you were born?");
    day=sc.nextLine();

    System.out.print("During what year were you born?");
    year=sc.nextInt();  
}

My code failed to compile. Can someone give me some hints and I will try it out on my own.

Perception
  • 79,279
  • 19
  • 185
  • 195
user2059072
  • 23
  • 1
  • 5

4 Answers4

4

Classes need declarations. Java is an OO language so using a class is a must:

class MyClass {
   public static void inputBirthday(int month, String day, int year) {
      ...
   }
}

Your inputBirthday could be replaced with a main method which would give your class an entry point from which to run your program.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
2

You need to encapsulate your method inputBirthday(...) inside class

like:

class Birthday{
     public static void inputBirthday(int month, String day, int year) {
      // .... rest of the code ....
  }
}
exexzian
  • 7,782
  • 6
  • 41
  • 52
2

In Java every thing need to be in a class.

For example

import java.util.Scanner;
public class BirthdayClass{
  public static void inputBirthday(){
    Scanner sc=new Scanner(System.in);
    System.out.print("On what day of the month were you born?");
    int month=sc.nextInt();

    System.out.print("What is the name of the month in which you were born?");
    int day=sc.nextLine();

    System.out.print("During what year were you born?");
    int year=sc.nextInt();

  }
 // This is main method which is called when class is loaded
 public static void main(String[] args){
   BirthdayClass.inputBirthday();
 }
}

Compile the program using javac BirthdayClass.java and then run it using java BirthdayClass.

Moreover you don't need to pass day,month and year as parameters as you are taking input in method. Rather you should declare them in the method.

kaysush
  • 4,797
  • 3
  • 27
  • 47
0

My suggestion is go back and read the first chapters in your book, you need to know these basic things to be able to do anything. Every program needs a main method somewhere, usually in a class by itself with little else in it. Eases the pain for debugging. But if your just making small excercises for school simply use this basic structure.

Obviously put it in a method if the excercise says so and call the method in the main method. You only build objects of something if you have it in a class by itself, but you probably aren't far enough in class to know about the OOP part yet.

className {

    public static void main(String[] args) {
        // your code here.    
    }


} // end of class.
ObedMarsh
  • 433
  • 4
  • 19