2

I am learning Java, and I'm not very far into it, and I don't know why but Java seemed to skip a line. I don't think the code from all my pages is really neccesery so I will just put the first page and the result I get when using it. Thanks!

import java.util.Scanner;

public class First {
    public static void main(String args[]){
        Scanner scanz = new Scanner(System.in);
        System.out.println("Hello, please tell me your birthday!");
        System.out.print("Day: ");
        int dayz = scanz.nextInt();
        System.out.print("Month: ");
        int monthz = scanz.nextInt();
        System.out.print("Year: ");
        int yearz = scanz.nextInt();
        System.out.println("Now, tell me your name!");
        System.out.print("Name: ");
        String namez = scanz.nextLine();
        Time timeObject = new Time(dayz,monthz,yearz);
        Second secondObject = new Second(namez,timeObject);

        System.out.println("\n\n\n\n\n" + secondObject);
    }
}

It skips the line

        String namez = scanz.nextLine();

Console output: (excuse the birthday bit, it is other stuff)

Hello, please tell me your birthday!
Day: 34
Month: 234
Year: 43
Now, tell me your name!
Name: 




My name is  and my birthday is 00/00/43

It doesn't give you a chance to give a name, it just skips straight past and takes the name as null. Please, if anyone could, tell me why! I want to learn Java, and this little annoyance is standing in my way.

Thanks!

Bluetiger6001
  • 141
  • 1
  • 3
  • 9

2 Answers2

4

The problem is that the nextLine gets any characters on the line, and the \n (newline character) is left over from the scanner inputs above.

So instead of letting you enter something new, it takes the \n as the input and continues.

To fix, just put two scanners back to back like this:

System.out.print("Name: ");
scanz.nextLine();
String namez = scanz.nextLine();

Just using:

String namez = scanz.next();

will work too, but will limit the names to be one word. (aka first name only)

Chris Mukherjee
  • 831
  • 9
  • 25
  • To be more specific, the first scanz.nextLine() could be placed anywhere after the "int yearz = scanz.nextInt();" line (does not need to be right before the second nextLine). It is simply required to get the rest of the input from nextInt() (since the newline character "\n" is left over). This will allow the second scanz.nextLine() to wait and get the new input. – Chris Mukherjee Jun 18 '13 at 00:40
  • I don't see why the other guy deleted his post :(. I was going to say your answer was right anyway, but if you're coming back, your answer also helped! Thanks, by the way. :). – Bluetiger6001 Jun 18 '13 at 00:40
  • By the way, once thing I don't get (since I'm learning :D) is that the scanner works if you take the input from the second scanner. I mean, surely it would be able to take the first input, then the \n\n\n\n would wreck the second scanner? Thanks. – Bluetiger6001 Jun 18 '13 at 00:44
  • I'll walk through an example: First scanner gets just int. Input = "34\n" Second scanner again gets just int. Input = "234\n" Third scanner again gets just int. Input = "43\n" Now, the first scanner for the name will get nextLine. Since the input "\n\n\n" is still there, this will be taken. In order to fix it, you have to add another nextLine. Since there are no leftover characters left, this scanner will now wait and get the line you enter as the name. – Chris Mukherjee Jun 18 '13 at 01:10
  • It is important to notice that when you print out the contents of a nextLine variable it will not print out characters such as \n (newline), \t (tab), etc. Those are special characters. – Chris Mukherjee Jun 18 '13 at 01:14
  • Okay, thank you. I still don't see exactly why it still has the \n\n\n when they should have already been used in the console to, you know, make a new line. Thanks, though. – Bluetiger6001 Jun 18 '13 at 01:14
2

I believe the intended use of nextLine is correct. The problem however is that nextInt does not create a newline token, and it's instead reading the rest of that line (which is empty). I believe that if another nextLine statement would be added after that, the code would work. Next on the other hand only recognizes the first word so that might not be the correct solution.

Jeroen Vannevel
  • 41,258
  • 21
  • 92
  • 157