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
45
votes
8 answers

How to read a text file directly from Internet using Java?

I am trying to read some words from an online text file. I tried doing something like this File file = new File("http://www.puzzlers.org/pub/wordlists/pocket.txt"); Scanner scan = new Scanner(file); but it didn't work, I am getting…
randomizertech
  • 2,199
  • 12
  • 46
  • 83
44
votes
6 answers

java.util.NoSuchElementException: No line found

I got an run time exception in my program while I am reading a file through a Scanner. java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Day1.ReadFile.read(ReadFile.java:49) at…
Ashish Panery
  • 1,016
  • 3
  • 13
  • 22
41
votes
5 answers

Close a Scanner linked to System.in

I have a Scanner linked to System.in. Now, after using the Scanner, I should close it, as it is bad coding practice to leave it open. But, if I close the Scanner, I will also be closing System.in! Can anyone tell me how I can close the Scanner…
JavaNewbie_M107
  • 1,897
  • 3
  • 19
  • 32
38
votes
5 answers

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() { reader = new Scanner(System.in); …
mateusmaso
  • 6,033
  • 6
  • 36
  • 54
37
votes
6 answers

Scanner doesn't see after space

I am writing a program that asks for the person's full name and then takes that input and reverses it (i.e John Doe - Doe, John). I started by trying to just get the input, but it is only getting the first name. Here is my code: public static void…
igeer12
  • 391
  • 1
  • 4
  • 8
36
votes
17 answers

How to read multiple Integer values from a single line of input in Java?

I am working on a program and I want to allow a user to enter multiple integers when prompted. I have tried to use a scanner but I found that it only stores the first integer entered by the user. For example: Enter multiple integers: 1 3 5 The…
Steven
  • 455
  • 1
  • 5
  • 8
35
votes
7 answers

Is there an equivalent to the Scanner class in C# for strings?

In Java I can pass a Scanner a string and then I can do handy things like, scanner.hasNext() or scanner.nextInt(), scanner.nextDouble() etc. This allows some pretty clean code for parsing a string that contains rows of numbers. How is this done in…
mmcdole
  • 86,293
  • 60
  • 181
  • 221
33
votes
4 answers

Is it safe not to close a Java Scanner, provided I close the underlying readable?

If I have a method that takes a reader and I want to operate on the reader with a Scanner like so: Scanner scanner = new Scanner(reader); while(scanner.hasNext()) { //blah blah blah } Is it safe not to close scanner? Documentation says that it…
Anthony
  • 11,352
  • 9
  • 65
  • 100
33
votes
5 answers

Read line with Scanner

EDIT for further readers: the problem was that my input file was corrupted. I don't understand what I'm doing wrong : I was using this code : File f = new File("C:\\Temp\\dico.txt"); BufferedReader r = null; try { r = new…
Sharcoux
  • 4,282
  • 4
  • 29
  • 65
31
votes
3 answers

Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

I am writing a simple program that prompts a user to enter a number of students, then asks the user to enter each student's name and score in order to determine which student has the highest score. I have written the program code and it compiles. …
user1011064
  • 321
  • 2
  • 4
  • 4
31
votes
1 answer

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. They all require the same basic code structure…
user177800
30
votes
5 answers

Read next word in java

I have a text file that has following content: ac und accipio annehmen ad zu adeo hinzugehen ... I read the text file and iterate through the lines: Scanner sc = new Scanner(new File("translate.txt")); while(sc.hasNext()){ String line =…
Upvote
  • 65,847
  • 122
  • 353
  • 577
30
votes
23 answers

Scanner doesn't read whole sentence - difference between next() and nextLine() of scanner class

I'm writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say only 'Archbishop' from 'Archbishop Street'. How do I fix this? import…
Bonett09
  • 345
  • 1
  • 3
  • 3
28
votes
6 answers

How to use Scanner to accept only valid int as input

I'm trying to make a small program more robust and I need some help with that. Scanner kb = new Scanner(System.in); int num1; int num2 = 0; System.out.print("Enter number 1: "); num1 = kb.nextInt(); while(num2 < num1) { System.out.print("Enter…
John
  • 3,883
  • 6
  • 23
  • 27
28
votes
4 answers

Scanner is never closed

I'm working on a game and I came across a little problem with my scanner. I'm getting a resource leak scanner never closed. But I thought my scanner was working before without closing it. But now it ain't. Anyone can help me out here? import…