How to Read Input from the Console Using the Scanner Class in Java

How can I read input from the console using the Scanner class in Java?

A simple example to illustrate how java.util.Scanner works would be reading a single integer from System.in. It's really quite simple.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

To retrieve a username I would probably use sc.nextLine().

System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);

You could also use next(String pattern) if you want more control over the input, or just validate the username variable.

You'll find more information on their implementation in the API Documentation for java.util.Scanner

Reading a character input in the console using the Scanner Class in Java

This method Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

char ch = sc.next(".").charAt(0);

In this code it waiting to match this from this input "."

How to read strings from a Scanner in a Java console application?

Scanner scanner = new Scanner(System.in);
int employeeId, supervisorId;
String name;
System.out.println("Enter employee ID:");
employeeId = scanner.nextInt();
scanner.nextLine(); //This is needed to pick up the new line
System.out.println("Enter employee name:");
name = scanner.nextLine();
System.out.println("Enter supervisor ID:");
supervisorId = scanner.nextInt();

Calling nextInt() was a problem as it didn't pick up the new line (when you hit enter). So, calling scanner.nextLine() after that does the work.

Scanner inputs in java not functioning

If you want to take user input then you should create an object and then use that object to take input.

Example:


Scanner input = new Scanner(System.in); //here input is the name of object int

x = input.nextInt(); //now the above expression take an integer as a input from user
//nextInt() is method of class Scanner

Use this tutorial to see all the methods of Scanner class

How to provide the input data for Scanner class from a text file instead of keyboard in java?

If you want to be able to run the same code, unchanged, for either getting user input via standard input or via a file, I suggest defining a "system" property as a flag to determine where user input comes from, depending on whether the property is defined or not.

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;

public class Testings {
public static void main(String[] args) throws IOException {
String redirect = System.getProperty("REDIRECT");
Scanner scanner;
if (redirect != null) {
scanner = new Scanner(Paths.get("", args));
}
else {
scanner = new Scanner(System.in);
}
if (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
if (redirect != null) {
scanner.close();
}
}
}

If you want input from a file, rather than from standard input, use the following command to run the code.

java -DREDIRECT Testings path/to/file.txt

If you want input from the user, via the keyboard then remove -DREDIRECT, i.e.

java Testings

Since you are not getting input from the file, no need to provide a path.

Note that if you are using an IDE, then it should provide the ability to define a "system" property as above. Also note that the property name can be anything. I simply (and arbitrarily) chose REDIRECT. Remember though, that it is case sensitive.

Alternatively, you can redirect System.in.

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class Testings {
private static void redirect(String[] args) throws IOException {
Path path = Paths.get("", args);
InputStream is = Files.newInputStream(path);
System.setIn(is);
}

public static void main(String[] args) throws IOException {
String redirect = System.getProperty("REDIRECT");
Scanner scanner;
if (redirect != null) {
redirect(args);
}
scanner = new Scanner(System.in);
if (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}
}

Regarding the file that you use for input, the path to the file can be either relative or absolute. If it is relative, then it is relative to the current working directory which is the value returned by the following code:

System.getProperty("user.dir");

Alternatively, you could make the file a resource which would mean that the path is relative to the CLASSPATH.

Not able to type input in cmd for scanner class input

The java.util.Scanner.close() method closes the Scanner. You can't take the input from the console if you have closed the Scanner object. So remove the scan.close() from your code and it will work like charm. Only close scanner object at the end.
More on Scanner class and its methods

Scanner input from terminal

Because scanner.next() splits your text from whitespaces, so you should use scanner.nextLine()

import java.util.Scanner;
public class DemoDate {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
System.out.println(text);
}
}


Related Topics



Leave a reply



Submit