문제

Here, I want to take few lines as input until only '0' is entered in one of the lines. And print these lines in the reverse order of how they were input. Howver, I'm facing difficulty in usage of the labeled break. I'm getting the following error:

PrintRevOrderLines.java:17: error: unreachable statement
                System.out.println("\nReversed order of lines is as follows..\n");
                ^
1 error

I am unable to understand why line 17 is unreachable. I know I can easily use only the 'break' (not the labeled break) statement, allow 'count' variable to be incremented one more than actually it should be and then, while printing, do like this: for(int i=count-1;i>=0;i--) and avoid printing 0, but i want to

  1. stop the value of count at the correct value and,
  2. know why line 17 is unreachable

My code is as follows:

import java.io.*;
class PrintRevOrderLines
{
    public static void main(String[] args) throws IOException
    {   
        int count = 0;
        String inputs[] = new String[100];
        System.out.println("Enter 0 and hit enter to stop inputting values..\n");
        BufferedReader B = new BufferedReader(new InputStreamReader(System.in));
        for(int i=0;;i++)
        thisblock:
        {
            inputs[i] = B.readLine();
            if(inputs[i].charAt(0)=='0'){break thisblock;}
            count++;
        }
        System.out.println("\nReversed order of lines is as follows..\n");
        for(int i=count;i>=0;i--)
        {
            System.out.println(" "+inputs[i]);
        }
    }
}
도움이 되었습니까?

해결책 2

What happen is that your for loop will never end, since you don't put a termination condition, nor you "break" it inside the body of the for. Try this and you will see:

for (int i = 0;; i++)
    System.out.println("here");
System.out.println("\nReversed order of lines is as follows..\n");

You will get the same "unrecheable code".

Edit:

Try this:

boolean flag = false;
for (int i = 0;; i++) {
    thisblock: {
        inputs[i] = B.readLine();
        if (inputs[i].charAt(0) == '0') {
            flag = true;
            break thisblock;
        }
        count++;
    }
    if (flag)
        break;
}

Output:

Reversed order of lines is as follows..

 0
 3
 2
 1

다른 팁

Your code structure is terribly messy for what should be a simple thing. Labelled breaks are virtually always a bad idea! Also if your number of inputs goes over 100 you will crash or if someone enters a number beginning with 0 you will exit early (for example 0.3).

The actual source of the problem is the fact that you are always branching back into the loop even on the break and you have no exit condition on the loop. Try this instead:

    List<String> inputs = new ArrayList();
    System.out.println("Enter 0 and hit enter to stop inputting values..\n");
    BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
    while (!(input = b.readLine()).equals("0")) {
         inputs.add(input);
    }

    System.out.println("\nReversed order of lines is as follows..\n");
    for(int i=inputs.size()-1;i>=0;i--) {
        System.out.println(" "+inputs.get(i));
    }

No limit on number, much cleaner structure, no unreachable code and no false positives on the exit.

(Incidentally I'd have a just had a blank entry exit rather than a 0, in which case you can just check for isEmpty() instead of equals("0"))

You can use break statements with the various loops but also alone with a label. In other words, the following works

label:
    break label;

In your code, the break thisblock breaks from the thisblock label, not from the loop. Given that and the fact that your for loop has no termination expression, the compiler knows that your loop will never end and therefore any code that comes after it will never execute.

I don't know why you have a label, but in this case, you can simply remove it.

for (int i = 0;; i++) {

    inputs[i] = B.readLine();
    if (inputs[i].charAt(0) == '0') {
        break;
    }
    count++;
}

Your break thisblock; is causing you to break out of thisblock (obviously) which, because of its placement inside the for loop, is causing the loop to iterate through again instead of exiting it.

Since there's no other way for that loop to terminate, you'll never be able to move beyond that part of the function. Notice that if you remove the currently unreachable part of the code, the next for will be highlighted instead.

Just use a plain break instead:

for(int i=0;i<100;i++) //i<100 check so you don't get an index exception when you that 101st character.
{
    inputs[i] = B.readLine();
    if(inputs[i].charAt(0)=='0'){break;}
    count++;
}

counts value is correct. If you input 5 characters and then a 0, count will be 5, but you need to initialise the next for loop like this:

for(int i=count-1;i>=0;i--)
{
    System.out.println(" "+inputs[i]);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top