Questions tagged [java.util.scanner]

A simple text scanner in the JDK which can parse primitive types and strings using regular expressions.

Use this tag for questions related to the Java API library class java.util.Scanner, a class added to the API in Java 5. The class is, according to the Javadoc:

a simple text scanner which can parse primitive types and strings using regular expressions.

Its source can range from a file, the console, or even a string. (This class replaces the older class java.util.StringTokenizer, which was used prior to Java 5 for the same purpose but has less functionality compared to Scanner).

Important canonical posts with this tag are:

Related tags:

5920 questions
796
votes
21 answers

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…
blekione
  • 8,666
  • 3
  • 15
  • 23
303
votes
12 answers

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 reads files efficiently by using a buffer to avoid physical disk operations. My…
Mads Mobæk
  • 31,014
  • 20
  • 67
  • 77
234
votes
15 answers

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…
Hock3yPlayer
  • 2,391
  • 2
  • 11
  • 9
160
votes
10 answers

Scanner vs. StringTokenizer vs. String.Split

I just learned about Java's Scanner class and now I'm wondering how it compares/competes with the StringTokenizer and String.Split. I know that the StringTokenizer and String.Split only work on Strings, so why would I want to use the Scanner for a…
Dave
  • 3,919
  • 5
  • 28
  • 34
131
votes
23 answers

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 as a String. Yet, it would not always work in every…
Ralph
  • 2,719
  • 7
  • 21
  • 43
97
votes
5 answers

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() { Scanner scanner = new Scanner(System.in); …
Taylor P.
  • 1,073
  • 1
  • 8
  • 5
93
votes
15 answers

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 and why?
AnnieOK
  • 1,220
  • 1
  • 9
  • 10
92
votes
6 answers

How to read from standard input line by line?

What's the Scala recipe for reading line by line from the standard input ? Something like the equivalent java code : import java.util.Scanner; public class ScannerTest { public static void main(String args[]) { Scanner sc = new…
Andrei Ciobanu
  • 11,585
  • 20
  • 75
  • 115
68
votes
4 answers

java.util.NoSuchElementException - Scanner reading user input

I'm new to using Java, but I have some previous experience with C#. The issue I'm having comes with reading user input from console. I'm running into the "java.util.NoSuchElementException" error with this portion of code: payment = sc.next(); //…
fortune
  • 1,255
  • 2
  • 13
  • 20
61
votes
5 answers

Why is hasNext() False, but hasNextLine() is True?

Question How is it that for a scanner object the hasNextLine() method returns true while the hasNext() method returns false? Note: Based on the input file, the hasNext() method is returning the result as expected; the hasNextLine() does not seem to…
DRich
  • 933
  • 1
  • 8
  • 23
61
votes
3 answers

How do I use a delimiter with Scanner.useDelimiter in Java?

sc = new Scanner(new File(dataFile)); sc.useDelimiter(",|\r\n"); I don't understand how delimiter works, can someone explain this in layman terms?
NoMoreErrors
  • 1,093
  • 2
  • 10
  • 12
59
votes
0 answers

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 void main(String[] args) { …
Eng.Fouad
  • 107,075
  • 62
  • 298
  • 390
53
votes
12 answers

Reading a .txt file using Scanner class in Java

I am working on a Java program that reads a text file line-by-line, each with a number, takes each number throws it into an array, then tries and use insertion sort to sort the array. I need help with getting the program to read the text file. I am…
user12074577
  • 1,181
  • 7
  • 24
  • 30
50
votes
6 answers

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 best way to do this?
bhavna raghuvanshi
  • 965
  • 4
  • 14
  • 16
46
votes
8 answers

Read CSV with Scanner()

My csv is getting read into the System.out, but I've noticed that any text with a space gets moved into the next line (as a return \n) Here's how my csv starts: first,last,email,address 1, address 2 john,smith,blah@blah.com,123 St.…
coffeemonitor
  • 11,838
  • 33
  • 93
  • 146
1
2 3
99 100