All Questions

421
votes
15answers
275509 views

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

I am using Scanner methods nextInt() and nextLine() for reading input. Basically, it looks like this: System.out.println("enter numerical value"); int option; option = input.nextInt();//read nume...
59
votes
0answers
44709 views

Scanner issue when using nextLine after nextXXX

I've faced an issue when I'm trying to get the user input using Scanner: import java.util.Scanner; public class Main { public static Scanner input = new Scanner(System.in); public static voi...
5
votes
1answers
6991 views

How to use java.util.Scanner to correctly read user input from System.in and act on it?

This is meant to be a canonical question/answer that can be used as a duplicate target. These requirements are based on the most common questions posted every day and may be added to as needed....
33
votes
6answers
206580 views

Validating input using java.util.Scanner

I'm taking user input from System.in using a java.util.Scanner. I need to validate the input for things like: It must be a non-negative number It must be an alphabetical letter ... etc What's the ...
133
votes
14answers
1040145 views

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 ...
43
votes
10answers
137776 views

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

What is the main difference between next() and nextLine()? My main goal is to read the all text using a Scanner which may be "connected" to any source (file for example). Which one should I choose an...
21
votes
5answers
21920 views

How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner

So, I'm getting stuck with this piece of code: import java.util.InputMismatchException; import java.util.Scanner; public class ConsoleReader { Scanner reader; public ConsoleReader() { ...
75
votes
5answers
552491 views

Using scanner.nextLine()

I have been having trouble while attempting to use the nextLine() method from java.util.Scanner. Here is what I tried: import java.util.Scanner; class TestRevised { public void menu() { ...
224
votes
15answers
196722 views

Scanner vs. BufferedReader

As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader. I also know that the BufferedReader read files efficiently by using ...
83
votes
17answers
631931 views

Take a char input from the Scanner

I am trying to find a way to take a char input from the keyboard. I tried using: Scanner reader = new Scanner(System.in); char c = reader.nextChar(); This method doesn't exist. I tried taking c a...

Previous Next