Week 9 — What is the (purpose of the) Scanner class?
Question of the Week #9
What is the (purpose of the) Scanner class?
7 Replies
The scanner class is used to read input from different places, such as user input!
Submission from Tryhrdsnphrd#6996
java.util.Scanner
allows reading textual input from System.in
, files or other resources. It provides utility methods for reading various types data, e.g.:
- nextInt
reads a numeric value and parses it to an int
- nextLong
reads a numeric value and parses it to a long
- nextDouble
reads a 64bit IEEE754 floating point number (double
).
- nextBoolean
for reading a boolean
(true
or false
) (this is not case sensitive)
- next
for reading a String. This stops as soon as a whitespace is reached.
- nextLine
for reading an entire line. If a different next
-method was called before, this continues reading the same line, even if it is empty.
- hasNext
returns true
if there is any token left to read - There are specific hasNext
methods (e.g. hasNextInt
) for specific checking for specific patterns.
Typically, tokens are separated by whitespaces or line breaks.
For example, take the following input (over System.in
)
It is possible to read that using the following code:
It is possible to pass any InputStream
to the Scanner
's constructor instead of System.in
.
It is also possible to construct a Scanner
reading from a file as follows:
As an alternative to
Scanner
, BufferedReader
can be used:
⭐ Submission from dan1st#7327
The purpose of the scanner class is to let the user input data into the console. For example:
Submission from Seantastik#2908
The Scanner class in Java is used to read input from various sources such as the keyboard, files, and network sockets. It provides a convenient way to parse input and convert it into different data types, such as int, double, String, etc.
1 :Reading input from the keyboard:
The Scanner class can be used to read input from the keyboard, making it easy to get user input in a Java application. For example:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
2 :Parsing strings:
The Scanner class can be used to parse a string into different data types, such as integers, doubles, and dates. For example:
String input = "100 200 300";
Scanner scanner = new Scanner(input);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
3:Reading from files:
The Scanner class can be used to read data from files, making it easy to read data from files in a Java application. For example:
Scanner scanner = new Scanner(new File("data.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
The Scanner class is a versatile class and is widely used in Java applications to read input from various sources.
⭐ Submission from Hassam#3933
The Scanner class is used to gain input from the user
It is from the java.utils package
Code Example:
import java.util.Scanner;
class scan {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
System.out.println(i);
}
}
if i is char then the input statement would be as follows:
char i=sc.next().charAt(0);
The scanner class can also be used to read text from file:
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
class scan {
public static void main(String[] args) throws IOException{
FileReader fr=new FileReader("C:/users/James/RandomFolder/Rfile.txt");
Scanner sc=new Scanner(fr);
while(sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
}
}
The above program reads all the lines of a file and outputs it
There that's my submission
Submission from EldritchHorror#7153