Is it considered bad practice to do this:
Scanner scan = new Scanner(System.in);
if(scan.nextInt() == 5) { //testing if input is equal to 5
System.out.println("input equals 5");
}
What about:
Scanner scan = new Scanner(System.in);
if(scan.nextInt() == scan.nextInt()) { //testing if two inputted ints are equal to each other
System.out.println("input1 equals input 2");
}
I read somewhere that this can cause "unexpected results" but I don't know what was meant by that. I have tested this quite a bit and haven't run into anything unexpected.
scanner.nextInt() == 7
, the test would read another value from theScanner
. This is a very common bug in the usage ofIterator
for example. So it's fine if you know want you're doing, but if you forget then...