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
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
Scanner scan = new Scanner(System.in);
String myLine = scan.nextLine();
How to read strings from a Scanner in a Java console application? - Stack Overflow
java - How to capture enter key, using scanner as console input? - Stack Overflow
Why is it printing the same thing twice?
Can't type user input for Java in Vs Code?
use code runner extension and add "code-runner.runInTerminal": true in vs code settings.
More on reddit.comScanner 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.
What you can do is use delimeter as new line. Till you press enter key you will be able to read it as string.
Scanner sc = new Scanner(System.in);
sc.useDelimiter(System.getProperty("line.separator"));
Hope this helps.
You can read line and if line is blank, You can assume it is enter key.. like below code..
Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
System.out.println(readString);
if (readString.equals(""))
System.out.println("Enter Key pressed.");
if (scanner.hasNextLine())
readString = scanner.nextLine();
else
readString = null;
I think this will help you
Scanner input= new Scanner(System.in);
String readString = input.nextLine();
while(readString!=null) {
System.out.println(readString);
if (readString.equals(""))
System.out.println("Read Enter Key.");
if (input.hasNextLine())
readString = input.nextLine();
else
readString = null;
}