0
I write this program and I want to program gets number from user data and it dosen't work with for example "apple 12345":
import java.util.Scanner; class Example { public static void main(String[]args) { Scanner abc = new Scanner(System.in); System.out.println(abc.nextInt()); } }
2 Answers
+ 2
Casting non-numeric strings to integers usually produces a 0/truncation in programs, at the first illegal character:
E.g. Typically
sample 12345 0 or null
123apple456 123
45 45 (spaces ok)
3 7 9 3 (internal spaces truncate)
Though you may get an exception, it is generally assumed you know the difference between string and numeric types.
+ 1
As you said, you want a number and the program is in fact expecting a nextInt
And apple is not a number XD