How To Read Input From Ascendance Trouble Inwards Coffee Using Scanner
Java five introduced a overnice utility called java.util.Scanner which is capable of reading input shape ascendance job inward Java. Using Scanner is a overnice together with gear upwards clean agency of retrieving user input from console or ascendance line. The scanner tin conduct keep InputStream, Reader or exactly path of the file from where to read input. In companionship to read from the ascendance line, nosotros tin transcend System.in into Scanner's constructor equally a rootage of input. The scanner offers several do goodness over classical BufferedReader approach, hither are around of the benefits of using java.util.Scanner for reading input from ascendance job inward Java:
1) The scanner supports regular expression, which gives y'all the ability to read alone matching the pattern from the input.
2) The scanner has methods similar nextInt(), nextFloat() which tin survive used to read numeric input from ascendance job together with tin survive straight used inward code without using Integer.parseInt() or whatever other parsing logic to convert String to Integer or String to double for floating betoken input.
Reading input from the ascendance job should survive the outset few affair which novel Java programmer should survive taught equally it helps them to write an interactive programme together with consummate programming practise similar checking for a prime number, finding the factorial of a number, reversing String etc. Once y'all are comfortable reading input from the ascendance job y'all tin write many interactive Java application without learning in GUI technology scientific discipline similar Swing or AWT.
Java programme to read input from ascendance prompts inward Java
Here is sample code instance of How to read input from the ascendance job or ascendance prompt in Java using java.util.Scanner class:
import java.io.IOException;
import java.util.Scanner;
public class ReadInputScannerExample {
public static void main(String args[]) throws IOException {
Scanner scanner = new Scanner(new InputStreamReader(System.in));
System.out.println("Reading input from console using Scanner inward Java ");
System.out.println("Please acquire inward your input: ");
String input = scanner.nextLine();
System.out.println("User Input from console: " + input);
System.out.println("Reading int from console inward Java: ");
int publish = scanner.nextInt();
System.out.println("Integer input: " + number);
}
}
Output:
Reading input from console using Scanner inward Java
Please acquire inward your input:
number
User Input from console: number
Reading int from console inward Java:
1232
Integer input: 1232
import java.util.Scanner;
public class ReadInputScannerExample {
public static void main(String args[]) throws IOException {
Scanner scanner = new Scanner(new InputStreamReader(System.in));
System.out.println("Reading input from console using Scanner inward Java ");
System.out.println("Please acquire inward your input: ");
String input = scanner.nextLine();
System.out.println("User Input from console: " + input);
System.out.println("Reading int from console inward Java: ");
int publish = scanner.nextInt();
System.out.println("Integer input: " + number);
}
}
Output:
Reading input from console using Scanner inward Java
Please acquire inward your input:
number
User Input from console: number
Reading int from console inward Java:
1232
Integer input: 1232
Just greenback that java.util.Scanner nextInt() volition throw "Exception inward thread "main" java.util.InputMismatchException" exception if y'all entered String or grapheme information which is non publish spell reading input using nextInt(). That’s all on how to read user input from the ascendance job or ascendance prompt inward Java. Further Learning
Complete Java Masterclass
How to run Java programme from ascendance line
Komentar
Posting Komentar