Use Scanner and Scanner.nextInt() method to take only Integer as input from the user.
Scanner sc = new Scanner(System.in);
int anyNumber = sc.nextInt();
If the user gives an input which is not an integer, it will throw an InputMismatchException.
You can use java.util.Scanner (API):
import java.util.Scanner;
//...
Scanner in = new Scanner(System.in);
int num = in.nextInt();
It can also tokenize input with regular expression, etc. The API has examples and there are many others in this site (e.g. How do I keep a scanner from throwing exceptions when the wrong type is entered?).
If you are using Java 6, you can use the following oneliner to read an integer from console:
int n = Integer.parseInt(System.console().readLine());
java - reading int from console - Stack Overflow
java - How to read an int from the console in a beginner-friendly way? - Stack Overflow
Java User Input and Difference between readInt and nextInt? - Stack Overflow
How to take console input in java using System.in.read().
I'm trying to recreate this textbook exercise but I'm having trouble reading integers off the console. I'm aware of the scanner class but I'm very insistent on making this Console.readInt() method work.
Java's official java.io.Console documentation doesn't list readInt as a method of the class. Why doesn't it?
int speed = Integer.parseInt(System.console().readLine());
Not sure how old that book is but this should be the correct way of doing it. As far as I know readInt isn't a function of console.
Now I see the issue. The book you're using shows Console().readLine() instead of System.console().readLine().
Integer.parseInt(String); is something that you want.
Try this:
int[] array = new int[size];
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int j = 0; j < array.length ; j++) {
int k = Integer.parseInt(br.readLine());
array[j] = k;
}
}
catch (Exception e) {
e.printStackTrace();
}
Anyways,why don't you use Scanner? It'd be much easier for you if you use Scanner. :)
int[] array = new int[size];
try {
Scanner in = new Scanner(System.in); //Import java.util.Scanner for it
for (int j = 0; j < array.length ; j++) {
int k = in.nextInt();
array[j] = k;
}
}
catch (Exception e) {
e.printStackTrace();
}
int x = Integer.parseInt(String s);
Try the following:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter some integer:\t");
int myIntValue = scanner.nextInt();
In order to use through the lifespan of your test application, you can do the following:
public void scanInput() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter some integer:\t");
int myIntValue = scanner.nextInt();
System.out.print("You entered:\t" + myInteValue);
}
}
Are you talking about reading user input from the console? You could use the Scanner class in that case.
Scanner stdin = new Scanner(System.in);
int input = stdin.nextInt();
If you want to grab different input, no need to initialize a new scanner. You can use a different method from the scanner in that case, depending on the value you want.
System.out.println("Enter a double: ");
double myDouble = stdin.nextDouble();
System.out.println("Enter a string of characters: ");
String myString = stdin.next();
System.out.println("Enter a whole line of characters: ");
String myEntireLine = stdin.nextLine();
etc.
You need something like a scanner to read-in values from console. The code should look like that:
import java.util.Scanner;
class TUI {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the two numbers:");
System.out.println("Enter n1:");
int n1 = scanner.nextInt();
System.out.println("Enter n2:");
int n2 = scanner.nextInt();
int total = n1 + n2;
System.out.println("Total is =" + total + ".");
scanner.close();
}
}
I hope it helps.
readInt is used with datainputstream class. nextInt with scanner class and you need to create a scanner object inorder to get input values. i.e
java.util.Scanner obj=new java.util.Scanner(System.in);
int x=obj.nextInt();
Objective of both is the same to read integer values.