Question:
Java Scanner Question?
cheezncrackers95
2011-09-28 22:16:50 UTC
I need to create code using scanners that would output the following:
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 might old!

I'm calling a method because this code is part of a larger program.
My main method includes:

Scanner console = new Scanner(System.in);
inputBirthday(console);
System.out.println();

This is my method so far, but I keep getting errors? Any help is greatly appreciated! :)

public static void inputBirthday
{
System.out.print("On what day of the month were you born?");
int day = console.nextInt();
System.out.print("What is the name of the month in which you were born?");
int month = console.next();
System.out.print("During what year were you born?");
int year = console.nextInt();
System.out.print("You were born on");
System.out.print(month + day + ',' + year + '.');
System.out.print("You're mighty old!");
}
}
Five answers:
auntjemimalol
2011-09-29 00:37:47 UTC
As you have it in your question." public static void inputBirthday"

{

I view methods as sub-programs, so going from that, you need to give the "sub programs" everything they need to operate. That's why you would pass arguments to methods. Going from you additional details, i can see that you already know that. But, the main thing you forgot to include was the reference type. For example, if i wanted to create a method that added 3 to input,n. I would write it like so: public static void (Integer n) {n+=3;} I would make the argument an int reference type and declare the name i will use to refer to this variable. (instead of Integer, most use ( int variableName). It actually converts from primitive to Integer reference type if I'm correct!) SO after this wall of text :P, the answer to your question would look like so:

//

//

public static void inputBirthday( Scanner console) {System.out.print("On what day of the month were you born?");

int day = console.nextInt();

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

int month = console.next();

System.out.print("During what year were you born?");

int year = console.nextInt();

System.out.print("You were born on");

System.out.print(month + day + ',' + year + '.');

System.out.print("You're mighty old!");}
green meklar
2011-09-28 22:38:44 UTC
As I recall, when you call Scanner.nextInt(), it extracts the next formatted integer but NOT the following newline character. This means that the scanner ends at a position before the newline character you typed. Then when you go to try to get the next input, it just sees that newline character immediately and gives you an empty input. You fix this by inserting a throwaway call to Scanner.next() after a call to Scanner.nextInt().



I might be mistaken, it's been a while since I ran into this sort of issue, but I think that's how you do it.
Vaibhav
2011-09-29 05:54:51 UTC
It should be

public static void inputBirthday( Scanner console )

Also as Meklar said you have to put a

scanner.next();

before getting month
modulo_function
2011-09-28 22:41:48 UTC
Getting errors, eh? Why keep it a secret, tell us what the errors say, preferably a copy & paste if it's a compile error or a runtime error.
2016-03-01 05:51:28 UTC
You shouldn't be comparing the answer with the equal sign ... use the compareTo method of the String class.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...