I've read through a bunch of threads on using break
and continue
and I suspect the problem isn't necessarily my use of those, but the layout of my loops. In the following code, I am trying to iterate through the chars in a string that is input by the user to find any -
symbols. If found, it will throw an error to the user that a negative number was found and exit. Otherwise, if it does not find a -
symbol, it should print out all of the chars in the string.
I used break
at the end of my first loop to find the -
symbol, but it is not continuing on to the next loop. I tried continue
as well but that didn't work. Loops are new to me so I may have this completely wrong, all I know is my first loop is working OK and will throw the error when it finds a -
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);
}
}