0

Im having some trouble with scanning user input in one of my first java programs. When I compile and run this, I am immediately prompted for input (i.e the command line stops and blinks). When I enter anything, the first line is printed, asking me to enter an integer. Then the second line is printed and I'm prompted to enter another value.

The output from this program is the first two values that I input. This is hard to explain, but it basically asks for 3 input values and only uses two.

import java.util.Scanner;

public class objects
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);

      System.out.println("Enter an integer please...");
      int input = sc.nextInt();

      System.out.println("Enter your name please...");
      String name = sc.nextLine();

      System.out.println("The read values: " + input + ", " + name);

      sc.close();
   }
}
  • Not possible it would ask you to enter three times. Paste your output. – SMA Dec 13 '15 at 15:25
  • 2
    Basically It asks only once, not three times. First time it reads the `int` and second time it reads the `carriage return` from the first input. – YoungHobbit Dec 13 '15 at 15:26
  • Im not sure how best to show my output, so here is a gyazo screenshot.https://gyazo.com/1b4b9f192211e35244bb7c6e2f718489 – new_Programmer Dec 13 '15 at 15:38
  • Can you please use another compiler like `javac` from the oracle JDK? Looks like the GNU compiler has some flaws. – Tom Dec 13 '15 at 16:04
  • Same error when using the javac compiler. Must be the virtual machine? – new_Programmer Dec 13 '15 at 16:25

4 Answers4

0

Put a System.out.flush() command after your println statements if you're reading from the console directly afterward

0

just use this:

  Scanner sc = new Scanner(System.in);      
  System.out.print ("Enter your name please... ");
  String name = sc.nextLine();
  System.out.print ("Enter an integer please... ");
  int input = sc.nextInt();
  System.out.println ("The read values: " + input + ", " + name);

i just moved the integer below the name and it sorta fixed it. hahaha

tyrion
  • 13
  • 1
  • 4
  • This still does not work for me, its the same error. I have even tried reading the intiger as a line then using Integer.parseInt, still the same problem. Creating a new scanner object each time I want to read a value ALSO gives me the same problem. Must be my virtual machine.. Not sure what else it could be. – new_Programmer Dec 13 '15 at 18:55
0

When you introduce a number you press enter key, nextInt() uses the number but the enter (\n) remains buffered. After this if you call again nextInt(), Java tries to convert \n into a number giving you a NumberFormatException, but if you invoke nextLine() they read the enter as empty string

Here you have a better explanation and one solution

Can't use Scanner.nextInt() and Scanner.nextLine() together

Community
  • 1
  • 1
OscarGz
  • 1
  • 3
  • It seems this is an error to do with my installation of VirtualBox. No matter what I try, the problem persists. Even if i try to only read ONE integer, it will ask me to input two values. – new_Programmer Dec 14 '15 at 03:05
0

It seems this is an error to do with my installation of VirtualBox. No matter what I try, the problem persists. Even if i try to only read ONE integer, it will ask me to input two values.

Thanks for everyone who tried to help, I learned a lot just trying to debug this.