Java Loops - Break? Continue?
I've read a bunch of threads when using break
and continue
, and I suspect that the problem is not with using them, but with the layout of my loops. In the following code, I am trying to iterate over the characters in a string entered by the user to find any characters -
. If found, it will raise an error for the user that a negative number was found and exited. Otherwise, if it doesn't find a character -
, it should print all characters in the string.
I used break
at the end of my first loop to find a character -
, but it doesn't go to the next loop. I tried continue
it but it didn't work. Loops are new to me, so I could be completely wrong, all I know is my first loop is working fine and will throw an error when found -
in the string.
strNum1 = JOptionPane.showInputDialog ("Enter Number String");
for (int i = 0; i < strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c == '-') {
System.out.println("Negative Digit Found - Exiting");
break;
}
}
for (int i = 0; i < strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c <= 9) {
System.out.println(c);
}
}
The break statement only breaks you into the first loop. To skip the second loop if a character is found -
, you can use some variable boolean
to indicate whether the second loop should be executed:
strNum1 = JOptionPane.showInputDialog ("Enter Number String");
boolean isValid = true;
for (int i=0; i<strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c == '-'){
System.out.println("Negative Digit Found - Exiting");
isValid = false;
break;
}
}
if (isValid) {
for (int i=0; i<strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c <= '9'){
System.out.println(c);
}
}
}
If you replace break
with return
, it will exit the whole method. It looks like this is what you want.
'break;' will stop the loop from which it was started, where 'continue;' will skip the current "iteration" in the loop.
for(int x = 0; x < 10; x++)
{
if (x == 5)
break;
// other code
}
// more other code
This will exit the loop after x == 5 rather than the 6th and 10th iterations.
for(int x = 0; x < 10; x++)
{
if (x == 5)
break;
// other code
}
// more other code
This will do every iteration except the 6th iteration.
But if you want to skip "// other code", you will need to use "return;" if your code is in a function, and it will skip the rest of the function, which in this case is "other other code".
break leaves the loop, where it continues to move to the next iteration.
Use the return statement instead of break if you don't want to run a second loop after the first.
All answers are good, but if you want to repeat the request until a valid value is received, you will need another loop to do this using labels:
negative: while(true) {
strNum1 = JOptionPane.showInputDialog ("Enter Number String");
for (int i=0; i<strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c == '-'){
System.out.println("Negative Digit Found - Exiting");
continue negative;
}
break negative;
}
}
You are not saying whether the number should be an integer, so I assume it is. If so, instead of using loops, the best way to validate input would be:
int num1;
try {
num1 = Integer.parseInt(strNum1);
catch (NumberFormatException e) {
//not a valid number, complain about it
}
if (num1<0) {
//negative number not allowed, complain about it
}
for (int i=0; i<strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c == '-'){
System.out.println("Negative Digit Found - Exiting");
break;
}
}
can be changed as
if(strNum1.charAt(0) != '-'){
for (int i=0; i<strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c <= 9){
System.out.println(c);
}
}
}
else {
//negative number found...
}
In this case, you can avoid unnecessary loop and break statements.