How can I read input from the console using the Scanner class in Java?

How could I read input from the console using the Scanner class? Something like this: System.out.println(“Enter your username: “); Scanner = input(); // Or something like this, I don’t know the code Basically, all I want is have the scanner read an input for the username, and assign the input to a String variable. 16 … Read more

Scanner is skipping nextLine() after using next() or nextFoo()?

I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this: System.out.println(“Enter numerical value”); int option; option = input.nextInt(); // Read numerical value from input System.out.println(“Enter 1st string”); String string1 = input.nextLine(); // Read 1st string (this is skipped) System.out.println(“Enter 2nd string”); String string2 = input.nextLine(); // Read 2nd string … Read more

Exception in thread “main” java.util.NoSuchElementException

You close the second Scanner which closes the underlying InputStream, therefore the first Scanner can no longer read from the same InputStream and a NoSuchElementException results. The solution: For console apps, use a single Scanner to read from System.in. Aside: As stated already, be aware that Scanner#nextInt does not consume newline characters. Ensure that these are consumed before attempting to call nextLine again by using Scanner#newLine(). See: Do not create multiple buffered wrappers on … Read more

NoSuchElementException with Java.Util.Scanner

NoSuchElementException Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration. http://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html How about this : if(input.hasNextInt() ) number1 = input.nextInt(); // if there is another number else number1 = 0; // nothing added in the input

Rock, Paper, Scissors Game Java

I would recommend making Rock, Paper and Scissors objects. The objects would have the logic of both translating to/from Strings and also “knowing” what beats what. The Java enum is perfect for this. public enum Type{ ROCK, PAPER, SCISSOR; public static Type parseType(String value){ //if /else logic here to return either ROCK, PAPER or SCISSOR … Read more

Why am I getting InputMismatchException?

Here you can see the nature of Scanner: double nextDouble() Returns the next token as a double. If the next token is not a float or is out of range, InputMismatchException is thrown. Try to catch the exception try { // … } catch (InputMismatchException e) { System.out.print(e.getMessage()); //try to find out specific reason. } UPDATE CASE … Read more

What’s the difference between next() and nextLine() methods from Scanner class?

I always prefer to read input using nextLine() and then parse the string. Using next() will only return what comes before the delimiter (defaults to whitespace). nextLine() automatically moves the scanner down after returning the current line. A useful tool for parsing data from nextLine() would be str.split(“\\s+”). String data = scanner.nextLine(); String[] pieces = data.split(“\\s+”); // Parse the pieces For more information regarding … Read more