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
17
votes
2 answers

java.util.scanner throws NoSuchElementException when application is started with gradle run

I have a created a simple java "echo" application that takes a user's input and shows it back to them to demonstrate the issue. I can run this application without trouble using IntelliJ's internal "run" command, and also when executing the compiled…
nickbdyer
  • 564
  • 5
  • 13
16
votes
1 answer

What does scanner.close() do?

Say I have the following example code: Scanner scan1 = new Scanner(System.in); // declaring new Scanner called scan1 int x = scan1.nextInt(); // scan for user input and set it to x System.out.println(x); // print the value of…
null
  • 1,770
  • 2
  • 17
  • 39
15
votes
5 answers

Use File or FileReader with Scanner?

Disclaimer: I've looked through all the questions I can find and none of them answers this exact question. If you find one please point me to it and be polite. So, the Oracle I/O tutorial opens a text file with Scanner as follows: new…
orbfish
  • 6,349
  • 12
  • 50
  • 71
15
votes
5 answers

Scanner only reads first word instead of line

In my current program one method asks the user to enter the description of a product as a String input. However, when I later attempt to print out this information, only the first word of the String shows. What could be the cause of this? My method…
Kristian
  • 1,179
  • 12
  • 29
  • 43
15
votes
1 answer

Java - Using multiple delimiters in a scanner

I'm using a scanner to take input and, hopefully, split it into chunks. I want it to split it up using whole word delimiters. So right now I have: Scanner scanner = new Scanner("1 imported bottle of perfume at 27.99"); …
R.B.
  • 245
  • 1
  • 2
  • 11
15
votes
3 answers

How do I keep a Scanner from throwing exceptions when the wrong type is entered?

Here's some sample code: import java.util.Scanner; class In { public static void main (String[]arg) { Scanner in = new Scanner (System.in) ; System.out.println ("how many are invading?") ; int a = in.nextInt() ; …
David
  • 13,509
  • 33
  • 71
  • 101
15
votes
3 answers

Java Multiple Scanners

I have a class that creates multiple Integer objects and puts them into a LinkedList as shown below: public class Shares implements Queue { protected LinkedList L; public Shares() { L = new LinkedList(); } …
Ryan Gibson
  • 273
  • 2
  • 3
  • 9
14
votes
2 answers

Java Scanner(File) misbehaving, but Scanner(FIleInputStream) always works with the same file

I am having weird behavior with Scanner. It will work with a particular set of files I am using when I use the Scanner(FileInputStream) constructor, but it won't with the Scanner(File) constructor. Case 1: Scanner(File) Scanner s = new Scanner(new…
kashiko
  • 173
  • 1
  • 6
14
votes
9 answers

Explain this line written in JAVA

In HACKERRANK this line of code occurs very frequently. I think this is to skip whitespaces but what does that "\r\u2028\u2029\u0085" thing mean scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
Mayank Bist
  • 181
  • 1
  • 1
  • 6
14
votes
2 answers

Java scanner usage with \R pattern (issue with buffer boundary)

Executive summary: Are there any caveats/known issues with \R (or other regex pattern) usage in Java's Scanner (especially regarding internal buffer's boundary conditions)? Details: Since I wanted to do some multi-line pattern matching on…
OzgurH
  • 418
  • 2
  • 10
14
votes
6 answers

Why does Java ask me to hit Enter again?

I've been racking my brain for a while trying to understand how Scanner works. So here's the code: Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String p =…
alekscooper
  • 711
  • 6
  • 17
14
votes
4 answers

How to use Scanner to read silently from STDIN in Java?

I want to make a Java program that reads a Password from STDIN silently. I mean, without outputting any pressed chars to the terminal and keeping it hidden from commandline history and the operating system processlist ps.
rfgamaral
  • 15,937
  • 49
  • 156
  • 269
14
votes
6 answers

Scanner method to get a char

What is the Scanner method to get a char returned by the keyboard in Java. like nextLine() for String, nextInt() for int, etc.
bizarrechaos
  • 171
  • 1
  • 2
  • 8
13
votes
1 answer

Weird behaviour with Scanner#nextFloat

Running the following in Eclipse initially caused Scanner to not recognize carriage returns in the console effectively blocking further input: price = sc.nextFloat(); Adding this line before the code causes Scanner to accept 0,23 (french notation)…
James P.
  • 17,929
  • 27
  • 89
  • 147
13
votes
3 answers

Java -- Closing Scanner and Resource Leak

I'm learning Java and working on some projects for fun. One issue that I have run in to is that when I use a Scanner object Eclipse warns me that: Resource Leak: 'scan' is never closed. So, I added a scan.close(); at the end of my code and that…
SuperCow
  • 1,143
  • 5
  • 16
  • 29