Java Scanner examples

Long live the Scanner class, a few examples for self-reference. 1. Read Input 1.1 Read input from the console. JavaScanner1.java package com.mkyong.io; import java.util.Scanner; public class JavaScanner1 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { System.out.print("Please enter your name: "); String input = scanner.nextLine(); System.out.println("name : " + input); …

Read more

Java – How to read input from console using Scanner

This example shows you how to read input from the console using the standard java.util.Scanner class. JavaScanner.java package com.mkyong.io; import java.util.Scanner; public class JavaScanner { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { System.out.print("Please enter your name: "); String input = scanner.nextLine(); System.out.println("name : " + input); System.out.print("Please enter your …

Read more

Java – How to read input from System.console()

In Java, we can use System.console() to read input from the console. JavaSample.java package com.mkyong.io; import java.io.Console; public class JavaSample { public static void main(String[] args) { // jdk 1.6 Console console = System.console(); System.out.print("Enter your name: "); String name = console.readLine(); System.out.println("Name is: " + name); System.out.print("Enter your password: "); // Reads a password …

Read more

JUnit 5 ConsoleLauncher examples

This article shows you how to use the JUnit 5 ConsoleLauncher to run tests from a command line. Tested with JUnit 5.5.2 junit-platform-console-standalone 1.5.2 1. Download JAR To run tests from the command line, we can download the junit-platform-console-standalone.jar from Maven central repository manually. P.S This example is using version 1.5.2. https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.5.2/ 2. Usages Usually, …

Read more

How to get the standard input in Java

Note This post is duplicated, please refer to this – 3 ways to read input from console in Java. A quick example to show you how to read the standard input in Java. package com.mkyong.pageview; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Test { public static void main(String[] args) { BufferedReader br = null; …

Read more

Java – How to read input from the console

In Java, there are three ways to read input from a console : System.console (JDK 1.6) Scanner (JDK 1.5) BufferedReader + InputStreamReader (Classic) 1. System.console Since JDK 1.6, the developer starts to switch to the more simple and powerful java.io.Console class. JavaConsole.java package com.mkyong.io; import java.io.Console; public class JavaConsole { public static void main(String[] args) …

Read more