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
13
votes
1 answer

Can't use Scanner.nextInt() and Scanner.nextLine() together

I have to get a string input and an integer input, but there order of input should be that integer comes first then user should be asked for string input Scanner in = new Scanner(System.in); input = in.nextLine(); k = in.nextInt(); …
Umer Hassan
  • 1,099
  • 4
  • 16
  • 23
13
votes
2 answers

Difference between buffered reader and file reader and scanner class

Can anyone explain me the difference between the class BufferedReader, FileReader and Scanner? and which one to use when I want to read a text file?
Charbel
  • 169
  • 1
  • 4
  • 14
13
votes
2 answers

Eclipse character encoding

I am using Scanner to scan a .txt document in Java. However, when I open the .txt document in Eclipse, I notice some characters are not being recognized, and they are replaced with something that looks like this: � These characters won't even let…
13
votes
5 answers

Exception in thread "main" java.util.NoSuchElementException

Whenever I run this, the chooseCave() function works fine with the in.nextInt(). When I choose the cave, the messages pop up at 2-second intervals, and then as soon as it gets past that part, it gives me the error: Exception in thread "main"…
Isaac Smith
  • 143
  • 1
  • 1
  • 4
12
votes
3 answers

How to quickly search a large file for a String in Java?

I am trying to search a large text file (400MB) for a particular String using the following: File file = new File("fileName.txt"); try { int count = 0; Scanner scanner = new Scanner(file); while(scanner.hasNextLine()) { …
Chief DMG
  • 123
  • 1
  • 1
  • 8
12
votes
4 answers

Why does Scanner implement Iterator?

I was just wondering why java.util.Scanner implements java.util.Iterator? Scanner implements the remove method and throws an UnsupportedOperationException. But shouldn't a class, when implementing an interface, fulfill the contract of the…
Thirumalai Parthasarathi
  • 4,251
  • 1
  • 22
  • 42
12
votes
6 answers

Scanning multiple lines using single scanner object

I a newbie to java so please don't rate down if this sounds absolute dumb to you ok how do I enter this using a single scanner object 5 hello how do you do welcome to my world 6 7 for those of you who suggest…
Creative_Cimmons
  • 237
  • 1
  • 2
  • 11
12
votes
5 answers

Why is Scanner slower than BufferedReader when reading from input?

I understand what is Scanner good for, and also when to use Scanner and when BufferedReader. I read a different, yet in some therm similar question Scanner vs. BufferedReader Why is Scanner so slow when I read from the input? I assume it has to do…
Gábor Csikós
  • 2,283
  • 6
  • 28
  • 52
12
votes
3 answers

Close Scanner without closing System.in

I'm trying to re-factor a large and frequently used part of my application into separate methods to make it easier to maintain. Some of these methods asks the user for input and does input validation, so I've used a Scanner and System.in But when I…
deepy
  • 399
  • 3
  • 20
11
votes
4 answers

What does Scanner input = new Scanner(System.in) actually mean?

Scanner input = new Scanner(System.in); Could you give me a detailed explanation on what the code above is doing step by step? I don't really understand how it works and how it links to me later being able to do this statement: int i =…
The Man
  • 139
  • 1
  • 1
  • 4
11
votes
3 answers

When should we use console class?

I was reading about Console class, and in the very first line, it was written New to Java 6 and when we are running Java SE 6 from command line, then we are typically using console class object So, which means we are implicitly using console…
Ravi
  • 28,657
  • 41
  • 110
  • 158
11
votes
1 answer

Scanner error with nextInt()

I am trying to use Scanner to get an int from the keyboard, but I getting the following error: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at…
spatara
  • 833
  • 3
  • 14
  • 28
10
votes
5 answers

Java's Scanner vs String.split() vs StringTokenizer; which should I use?

I am currently using split() to scan through a file where each line has number of strings delimited by '~'. I read somewhere that Scanner could do a better job with a long file, performance-wise, so I thought about checking it out. My question is:…
APARI
10
votes
2 answers

What is the equivalent of Java Scanner in Kotlin?

What is the equivalent of Java Scanner in Kotlin? I have used readLine() but I'd like to know whether it's type safe or not?
esu
  • 1,470
  • 6
  • 16
  • 28
10
votes
3 answers

Java: scanning string for a pattern

This is probably a quicky. Why does this code not return anything? import java.util.Scanner; public class MainClass { public static void main(String[] args) { try { Scanner sc = new Scanner("asda ASA adad"); String pattern =…
Markos Fragkakis
  • 7,330
  • 16
  • 61
  • 100