java - How to use Scanner to accept only valid int as input - Stack Overflow
How to get the user input in Java? - Stack Overflow
java.util.scanner - How can I read input from the console using the Scanner class in Java? - Stack Overflow
How to use 'scanner' in JAVA?
Videos
I understand that when we initialize a scanner class with "System.in" it reads from the input stream. But I guess that it's purpose is to just save the given input to be used or saved in a variable. How does it request an input from the user? Why can't I give an input on my own? It's just pure curiosity, if anyone know i'd like to know :)
Use Scanner.hasNextInt():
Returns
trueif the next token in this scanner's input can be interpreted as anintvalue in the default radix using thenextInt()method. The scanner does not advance past any input.
Here's a snippet to illustrate:
Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
while (!sc.hasNextInt()) sc.next();
int num1 = sc.nextInt();
int num2;
System.out.print("Enter number 2: ");
do {
while (!sc.hasNextInt()) sc.next();
num2 = sc.nextInt();
} while (num2 < num1);
System.out.println(num1 + " " + num2);
You don't have to parseInt or worry about NumberFormatException. Note that since the hasNextXXX methods don't advance past any input, you may have to call next() if you want to skip past the "garbage", as shown above.
Related questions
- How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)
- the condition num2 < num1 should be num2 <= num1 if num2 has to be greater than num1
- not knowing what the kb object is, I'd read a
Stringand thentryingInteger.parseInt()and if you don'tcatchan exception then it's a number, if you do, read a new one, maybe by setting num2 to Integer.MIN_VALUE and using the same type of logic in your example.
One of the simplest ways is to use a Scanner object as follows:
import java.util.Scanner;
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt(); // Scans the next token of the input as an int
// Once finished
reader.close();
You can use any of the following options based on the requirements.
Scanner class
import java.util.Scanner;
//...
Scanner scan = new Scanner(System.in);
String s = scan.next();
int i = scan.nextInt();
BufferedReader and InputStreamReader classes
import java.io.BufferedReader;
import java.io.InputStreamReader;
//...
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int i = Integer.parseInt(s);
DataInputStream class
import java.io.DataInputStream;
//...
DataInputStream dis = new DataInputStream(System.in);
int i = dis.readInt();
The readLine method from the DataInputStream class has been deprecated. To get String value, you should use the previous solution with BufferedReader
Console class
import java.io.Console;
//...
Console console = System.console();
String s = console.readLine();
int i = Integer.parseInt(console.readLine());
Apparently, this method does not work well in some IDEs.
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();
I have to learn how to code in JAVA for school, I have been trying for many hours now but I fail to understand how to to use 'Scanner'
Could anybody help me out with this code to at least teach me how I can use it right the next time? Because the answers to my mistakes is nowhere to be found even after watching countless of Youtube videos.
I have to change the code so that the number will get printed.
Sample input1: 12 > The sample output for this has to be also: 12
Sample input 2: 42 > The sample output for this also has to be: 42
Sample imput 3: -12 > The sample output for this also has to be: -12
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = New Scanner(System.in);
int getal = scanner.nextInt();
System.out.println(........)
And then I honestly have no clue what to do..
Please help and thank you in advance