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
Eric McIntyre
Eric McIntyre2y ago
The scanner class is used to read input from different places, such as user input!
Submission from Tryhrdsnphrd#6996
Eric McIntyre
Eric McIntyre2y ago
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)
123 456 abc def
a b c
def
123 456 abc def
a b c
def
It is possible to read that using the following code:
try(Scanner scan = new Scanner(System.in)){
int firstInt = scan.nextInt();//123
int secondInt = scan.nextInt();//456
String firstWord = scan.next();//abc
String secondWord = scan.next();//def
String remainderOfLine = scan.nextLine();//this reads an empty line as nothing is left in that line after abc
String secondLine = scan.nextLine();//a b c
String thirdLine = scan.nextLine();//def
//do something with those
}//System.in is closed with the scanner and cannot be used afterwards
try(Scanner scan = new Scanner(System.in)){
int firstInt = scan.nextInt();//123
int secondInt = scan.nextInt();//456
String firstWord = scan.next();//abc
String secondWord = scan.next();//def
String remainderOfLine = scan.nextLine();//this reads an empty line as nothing is left in that line after abc
String secondLine = scan.nextLine();//a b c
String thirdLine = scan.nextLine();//def
//do something with those
}//System.in is closed with the scanner and cannot be used afterwards
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:
try(Scanner scan = new Scanner(Path.of("someFile.txt"),StandardCharsets.UTF_8)){
//read stuff with it
}
try(Scanner scan = new Scanner(Path.of("someFile.txt"),StandardCharsets.UTF_8)){
//read stuff with it
}
Eric McIntyre
Eric McIntyre2y ago
As an alternative to Scanner, BufferedReader can be used:
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
String[] firstLineSplit = reader.readLine().split("\\s+");
int firstInt = Integer.parseInt(firstLineSplit[0]);//123
int secondInt = Integer.parseInt(firstLineSplit[1]);//456
String firstWord = firstLineSplit[2];//abc
String secondWord = firstLineSplit[3];//def
boolean hasMoreTokensInFirstLine = firstLineSplit.length > 4;//false
String secondLine = reader.readLine();//a b c
String thirdLine = reader.readLine();//def
//do something with those
}//System.in is closed with the scanner and cannot be used afterwards
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
String[] firstLineSplit = reader.readLine().split("\\s+");
int firstInt = Integer.parseInt(firstLineSplit[0]);//123
int secondInt = Integer.parseInt(firstLineSplit[1]);//456
String firstWord = firstLineSplit[2];//abc
String secondWord = firstLineSplit[3];//def
boolean hasMoreTokensInFirstLine = firstLineSplit.length > 4;//false
String secondLine = reader.readLine();//a b c
String thirdLine = reader.readLine();//def
//do something with those
}//System.in is closed with the scanner and cannot be used afterwards
⭐ Submission from dan1st#7327
Eric McIntyre
Eric McIntyre2y ago
The purpose of the scanner class is to let the user input data into the console. For example:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
System.out.println(text);
}
}

import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
System.out.println(text);
}
}

Submission from Seantastik#2908
Eric McIntyre
Eric McIntyre2y ago
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
Eric McIntyre
Eric McIntyre2y ago
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
Eric McIntyre
Eric McIntyre2y ago
There that's my submission
Submission from EldritchHorror#7153
Want results from more Discord servers?
Add your server