1

I have written code that changes uppercase letters to lowercase letters and vice versa. I'm trying to use break so that I can exit the loop whenever the input is '.', and it doesn't seem to be working. Any advice would be appreciated! I also want to create a count of all the time the loops reiterated before it ended. How would I go about that?

public static void main(String[] args) throws java.io.IOException {

    char choice, ignore = 0;
    for (; ; ) {
        do {
            System.out.print("Please enter a upper or " + "lower case letter(. to quit)");
            choice = (char) System.in.read();
            if ((char) choice > 64) {
                ignore = (char) (choice + 32);
            }
            if ((char) choice > 96) {
                ignore = (char) (choice - 32);
            }
            System.out.print(ignore);
            System.out.println("\n");
            if (choice == '.') {
                break;
            }
            do {
                ignore = (char) System.in.read();
            } while (ignore != '\n');
        } while ((char) choice > 64 | (char) choice < 123 | choice != '.');
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
Parry Chen
  • 49
  • 1
  • 6
  • 3
    Use a do-while loop, not a `for (;;)` loop – Hovercraft Full Of Eels Jul 18 '16 at 15:20
  • 1
    You should compose your logic better to avoid "infinite loops" that require you to break. The loop should end conditionally – Vince Jul 18 '16 at 15:21
  • 1
    You're using nested loops. See http://stackoverflow.com/q/886955/4793951 for suggestions. – Zircon Jul 18 '16 at 15:23
  • 1
    @Zircon That question may answer this main question here, but OP should get rid of the outer loop to fix his problem, since the outer loop is the problem, not the `break`. – Tom Jul 18 '16 at 15:24
  • 1
    @Tom I agree, and the answers in the linked question recommend that as well. – Zircon Jul 18 '16 at 15:25
  • @Zircon *"he answers in the linked question recommend that as well"* Awesome :D. – Tom Jul 18 '16 at 15:26
  • Can I suggest that using `if (Character.isUpperCase(choice)) { ignore = (char) Character.toLowerCase(choice); }` (etc) is a more readable way of expressing the logic than all that casting an arithmetic. – Andy Turner Jul 18 '16 at 15:41

4 Answers4

1

The problem is you have a nested loop. Your for (; ; ) will not be exited because the break will only escape from:

do {
    //other code
} while ((char) choice > 64 | (char) choice < 123 | choice != '.');

You have a few options but probably the easiest would be to change the for loop to something like this:

while(choice != '.'){
     //do other code
}

This way when the break is reached your code will exit the do while and exit the while on the next loop.

Keep in mind with this technique you will have to initialize choice with a value.

duncan
  • 1,161
  • 8
  • 14
0

You have a do-while loop inside a permanent for-loop. The break statement is going to break you out of the do-while loop, but you're still in the for-loop. It will just re-enter the do-while loop again and keep going.

DeeV
  • 35,865
  • 9
  • 108
  • 95
0

Even though your while loop breaks, the for loop keeps on running forever. One fix may be to add the terminating condition within for loop.

 for (; choice != '.'; ) 

Please make sure to initialize the choice variable or else it would throw error during compile.

niraj
  • 17,498
  • 4
  • 33
  • 48
0

stackoverflow.com/q/886955/4793951 Break labels Thanks! @Zircon

Parry Chen
  • 49
  • 1
  • 6