The Java scanner example shows you how to read data from console in java and introduces the range of functionality of java.util.Scanner class. Java scanner can parse primitive types and strings using regular expressions. With a simple text, scanner breaks it into tokens using delimiter pattern, default token matches whitespace.
Here is first Java scanner example which use default token whitespace.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.javabycode.io; import java.util.Scanner; public class ScannerExample { public static void main(String[] args) throws Exception { String input = "1 dog or 2 dogs better"; Scanner s = new Scanner(input); while(s.hasNext()){ System.out.println(s.next()); } s.close(); } } |
One more java scanner example that can also use delimiters other than whitespace. It reads several items in from a string:
String input = “1 dog or 2 dogs better”;
Scanner s = new Scanner(input).useDelimiter(“\s*dog\s*”);
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
Other java scanner example which allows us to read a number from System.in:
1 2 |
Scanner sc = new Scanner(System.in); int i = sc.nextInt(); |
You can use the next methods to read data. The method you use depends on the type of data you need to read. To read string data, for example, you use the next method. To read integer data, you use the nextInt method. To read byte data, you use the nextByte method. To read double data, you use the nextDouble method. And to read all of the data on a line, you use the nextLine method. And and many other methods you can use
Full Java Scanner example
In this example, let us together find out how to read data from console in java. When the program step over a method of the Scanner class, the application waits for the user to enter data with the keyboard. To complete the entry, the user presses the Enter key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javabycode.io; import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { // Create a Scanner object try (Scanner scanner = new Scanner(System.in)) { // Read values from Console // A String value System.out.print("Enter Product Code: "); String productCode = scanner.next(); // A double value System.out.print("Enter Product Price: "); double price = scanner.nextDouble(); // An int value System.out.print("Enter Quantity: "); int quantity = scanner.nextInt(); // Display entered values double total = price * quantity; System.out.println("ProductCode | Quantity | Price | Total"); System.out.printf(" %s %d %.2f %.2f", productCode, quantity, price, total); } catch (Exception e) { e.printStackTrace(System.err); } } } |
Let’s dig deeper
public Scanner(InputStream source) – Constructs a new Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the underlying platform’s default charset.
public String next() – Finds and returns the next complete token from this scanner.
public double nextDouble() – Scans the next token of the input as a double.
public int nextInt() – Scans the next token of the input as an int.
Run the above code then enter data and complete entry, we will get the results like below screen shoot:
That’s all on the Java Scanner example post. And hope that now you can get how to read data from console in java.