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

Regular Expression to Match " | "

I am trying to use Java's useDelimiter method on it's Scanner class to do some simple parsing. Basically each line is a record delimited by " | ", so for example: 2 | John Doe 3 | Jane Doe 4 | Jackie Chan The method takes as a parameter a regular…
Jorge Israel Peña
  • 32,778
  • 15
  • 83
  • 118
10
votes
3 answers

Delimiter in Scanner Java confusion

According to Java API Scanner uses delimiters to break the whole input into tokens. I am trying to understand the tokens and delimiters. I was doing this program and hit a confusion import java.util.Scanner; public class Test { public static…
Aseem Bansal
  • 6,125
  • 11
  • 43
  • 80
10
votes
3 answers

Java Scanner question

How do you set the delimiter for a scanner to either ; or new line? I tried: Scanner.useDelimiter(Pattern.compile("(\n)|;")); But it doesn't work.
Razvi
  • 2,578
  • 5
  • 27
  • 38
10
votes
1 answer

how to insert a new line character in a string to PrintStream then use a scanner to re-read the file

I have several classes designed to simulation a book catalog. I have a book class (isbn, title, etc...), a BookNode class, a BookCatalog which is a LinkedList of books and a driver class (gui). My problem is that I have a toString() method in…
Sara
  • 137
  • 1
  • 2
  • 8
10
votes
4 answers

How to check the end of line using Scanner?

I have searched similar questions, but none helped. Consider a file : hi how are you? where were you? I want to do few operations after the end of every line. If I use next() it wont tell me when I have reached the end of the first line. Also I…
user1806722
10
votes
3 answers

Issues with nextLine();

Possible Duplicate: Scanner issue when using nextLine after nextInt I am trying create a program where it lets the user inputs values into an array using scanner. However, when the program asks for the student's next of kin, it doesn't let the…
jl90
  • 599
  • 2
  • 6
  • 23
9
votes
5 answers

Why does "hello\\s*world" not match "hello world"?

Why does this code throw a InputMismatchException ? Scanner scanner = new Scanner("hello world"); System.out.println(scanner.next("hello\\s*world")); The same regex matches in http://regexpal.com/ (with \s instead of \\s)
Navin
  • 2,866
  • 2
  • 23
  • 46
9
votes
6 answers

how to read a text file using scanner in Java?

This is my code to read a text file. When I run this code, the output keeps saying "File not found.", which is the message of FileNotFoundException. I'm not sure what is the problem in this code. Apparently this is part of the java. For the whole…
user764073
  • 91
  • 1
  • 1
  • 3
9
votes
3 answers

How to interrupt java.util.Scanner nextLine call

I am using a multi threaded environment were one Thread is constantly listening for user input by repeatedly calling scanner.nextLine(). To end the application, this runloop is stopped by another thread, but the listening thread won't stop until a…
j-pb
  • 792
  • 2
  • 7
  • 11
9
votes
9 answers

how to read int,double,and sentence of string using same scanner variable

import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=new String(); int x=sc.nextInt(); double y=sc.nextDouble(); …
Gokul Raj
  • 111
  • 1
  • 1
  • 2
9
votes
3 answers

How to use the same Scanner across multiple classes in Java

I have a program that uses multiple classes, I want the other classes to be able to access the same scanner that I have declared in the main class, I assume it would be done using some sort of get method, however I am unable to find any resources to…
Samolivercz
  • 200
  • 2
  • 10
9
votes
3 answers

Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()

My professor tends to do the following to get a number from the user: Scanner scanner = new Scanner(System.in); Integer.parseInt(scanner.nextLine()); What are the benefits as opposed to simply doing scanner.nextInt() ? java.util.Scanner.java has…
Olavi Mustanoja
  • 1,975
  • 2
  • 18
  • 30
9
votes
3 answers

Scanner input validation in while loop

I've got to show Scanner inputs in a while loop: the user has to insert inputs until he writes "quit". So, I've got to validate each input to check if he writes "quit". How can I do that? while (!scanner.nextLine().equals("quit")) { …
Kurt Bourbaki
  • 9,882
  • 6
  • 28
  • 46
9
votes
2 answers

Scanner NoSuchElementException

I'm having a problem with my Java assignment. I'm getting an unexpected exception, specifically: java.util.NoSuchElementException: No line found I am using Scanner(System.in) and the program is continually reading nothing and repeating the…
user2175782
  • 95
  • 1
  • 1
  • 3
9
votes
4 answers

Exception in thread "main" java.io.FileNotFoundException: Error

I am using Eclipse to compile and run my java codes. Here is Error I am getting. Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at…
Mowgli
  • 3,024
  • 20
  • 60
  • 84