You cannot declare a new variable in a loop while.
while (boolean always = true) {
}
You need to declare a variable before and outside the loop, so something like this is possible:
boolean always = true;
while (always) {
break;
}
always = !always;
In this sense, the loop foris unique: you can declare a new local variable whose scope is limited to this loop:
for (boolean always = true; always; ) {
break;
}
always = !always;
However, looking at what you are doing, you can look java.util.Scanner. I suspect that it will serve your needs much better.
Example
Scanner , 0. . , hasNextInt() Integer.parseInt/NumberFormatException.
Scanner sc = new Scanner(System.in);
System.out.println("Enter numbers (0 to end):");
int sum = 0;
int number;
do {
while (!sc.hasNextInt()) {
System.out.println("I'm sorry, that not a number! Try again!");
sc.next();
}
number = sc.nextInt();
sum += number;
} while (number != 0);
System.out.println("The sum of those numbers is " + sum);
:
Enter numbers (0 to end):
1
3
-1
five
I'm sorry, that not a number! Try again!
2
0
The sum of those numbers is 5