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
27
votes
5 answers

Java Scanner class reading strings

I got the following code: int nnames; String names[]; System.out.print("How many names are you going to save: "); Scanner in = new Scanner(System.in); nnames = in.nextInt(); names = new String[nnames]; for (int i = 0; i < names.length; i++){ …
marcoamorales
  • 595
  • 2
  • 8
  • 13
25
votes
2 answers

Difference between BufferedReader and BufferedInputStream

What are the differences between BufferedReader , BufferedInputStream and Scanner in java? BufferedReader reads the text and BufferedInputStream reads byte. Is there any difference other than this?
user1357722
  • 5,598
  • 12
  • 32
  • 41
24
votes
2 answers

Java using scanner enter key pressed

I am programming using Java. I am trying write code which can recognize if the user presses the enter key in a console based program. How can I do this using java. I have been told that this can be done using either Scanner or, buffered input…
H J
  • 309
  • 2
  • 4
  • 10
23
votes
1 answer

Java Scanner doesn't wait for user input

I am using Java's Scanner to read user input. If I use nextLine only once, it works OK. With two nextLine first one doesnt wait for user to enter the string(second does). Output: X: Y: (wait for input) My code System.out.print("X: "); x =…
user2976389
  • 397
  • 1
  • 2
  • 7
23
votes
8 answers

Extract Integer Part in String

What is the best way to extract the integer part of a string like Hello123 How do you get the 123 part. You can sort of hack it using Java's Scanner, is there a better way?
Verhogen
  • 23,861
  • 32
  • 81
  • 109
20
votes
4 answers

Java Scanner String input

I'm writing a program that uses an Event class, which has in it an instance of a calendar, and a description of type String. The method to create an event uses a Scanner to take in a month, day, year, hour, minute, and a description. The problem…
HolidayTrousers
  • 225
  • 1
  • 2
  • 8
20
votes
2 answers

How to determine when end of file has been reached?

I am trying to read text from a text file. I need help figuring out when the end of file has occured. How can I determine this in Java? FileInputStream istream = new FileInputStream("\""+filename+"\""); Scanner input = new…
Blackbinary
  • 3,674
  • 15
  • 45
  • 61
19
votes
5 answers

How to test for blank line with Java Scanner?

I am expecting input with the scanner until there is nothing (i.e. when user enters a blank line). How do I achieve this? I tried: while (scanner.hasNext()) { // process input } But that will get me stuck in the loop
Jiew Meng
  • 74,635
  • 166
  • 442
  • 756
19
votes
11 answers

How to put a Scanner input into an array... for example a couple of numbers

Scanner scan = new Scanner(System.in); double numbers = scan.nextDouble(); double[] avg =..????
WM.
  • 2,629
  • 8
  • 29
  • 34
19
votes
4 answers

Java: Infinite loop using Scanner in.hasNextInt()

I am using the following code: while (invalidInput) { // ask the user to specify a number to update the times by System.out.print("Specify an integer between 0 and 5: "); if (in.hasNextInt()) { // get the update value …
Tomek
  • 4,527
  • 14
  • 40
  • 48
18
votes
8 answers

Java scanner not going through entire file

I'm writing a program in Java and one of the things that I need to do is to create a set of every valid location for a shortest path problem. The locations are defined in a .txt file that follows a strict pattern (one entry per line, no extra…
Fizzmaister
  • 183
  • 1
  • 1
  • 5
18
votes
6 answers

Multiple delimiters in Scanner class of Java

How do I use the useDelimiter() method of the Scanner class to use both the comma (,) and the new line character (\n) as delimiters? I am parsing some text from a csv file.
tomejuan
  • 181
  • 1
  • 1
  • 3
18
votes
8 answers

NoSuchElementException with Java.Util.Scanner

I am very new to Java but am working through the book Java: How to program (9th ed.) and have reached an example where for the life of me I cannot figure out what the problem is. Here is a (slightly) augmented version of the source code example in…
adaam
  • 3,616
  • 5
  • 24
  • 51
18
votes
3 answers

Read input line by line

How do I read input line by line in Java? I searched and so far I have this: import java.util.Scanner; public class MatrixReader { public static void main(String[] args) { Scanner input = new Scanner(System.in); while…
spacitron
  • 379
  • 1
  • 2
  • 14
17
votes
5 answers

Why does hasNextLine() never end?

Sorry if this sounds too simple. I'm very new to Java. Here is some simple code I was using to examine hasNextLine(). When I run it, I can't make it stop. I thought if you didn't write any input and pressed Enter, you would escape the while…
Helgi
  • 328
  • 2
  • 3
  • 10