No. Only sensible strings of digits will work.
Here is the relevant line from the program:
num = scan.nextInt();
Assignment statements work in two steps:
In this particular assignment statement, the expression on the right
scans the next several characters of the input stream
and converts then into an int
, if they are correct.
Then the numeric result is stored into num
.
If the next group of characters cannot be converted,
Java throws an Exception
and stops your program.
An Exception
object contains information about
what went wrong in a program.
Industrial-strength programs may examine the exception and try
to fix the problem.
Our programs (for now) will just stop.
Which of the following inputs would be correct input for the program?
Enter an integer: 1492 Enter an integer: Fourteen ninety two Enter an integer: 14.92 Enter an integer: -1492 Enter an integer: 1 4 9 2