0

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]);
        }
    }
}
vipulnj
  • 135
  • 1
  • 4
  • 9

4 Answers4

2

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"))

Tim B
  • 40,716
  • 16
  • 83
  • 128
1

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
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Yea, but how do I break out of the loop and jump to `System.out.println("\nReversed order of lines is as follows..\n");` line ? I am unable to understand where/how I should place my break statement and the label itself. – vipulnj Jan 05 '14 at 21:54
  • One way would be adding a variable to test when you have to break. See edit. – Christian Tapia Jan 05 '14 at 21:58
  • I replaced your code with what I had typed. It no longer gives the unreachable statement error. Can you point out where I went wrong the usage of labeled break. Pretty embarassing coz labeled break is very easy concept but it's really testing me here.. – vipulnj Jan 05 '14 at 22:21
  • `break thisblock` just breaks that labeled block, not the `for` loop. With that labeled `break` you can't break two blocks (your labeled one and the `for`) at once. So you need to add an extra check to handle the "for break". I hope you understand, is a little hard to explain for me. – Christian Tapia Jan 05 '14 at 23:01
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++;
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

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]);
}
splrs
  • 2,424
  • 2
  • 19
  • 29
  • I tried putting the label after the loop in the `System.out.println("\nReversed order of lines is as follows..\n");` line but the same error occurs. I am unable to understand where/how I should place my break statement and the label. – vipulnj Jan 05 '14 at 21:57
  • The break's placement is okay, but since you're only looking to exit the loop you don't use the label. – splrs Jan 05 '14 at 21:59
  • Yea, I did try doing the same as you have suggested here before, but the value of `count` variable was one more than what it should have actually been. So, I thought it's breaking only out of the if block and used the labeled break to break out of both 'if' and 'for' blocks. – vipulnj Jan 05 '14 at 22:07
  • That's not related to this problem. Just initialise the next for loop with `int i=count-1` instead of `int i=count`. The value of count is correct, but the last element of an array/etc is accessed as `count-1`. Remember you're using 0-based indexing. – splrs Jan 05 '14 at 22:12
  • Yea @splrs I did that and did recieve the correct answer, no doubt about that but I wanted to know why the value of count is not stopping where it should, as I've mentioned in the description above! :) – vipulnj Jan 05 '14 at 22:14
  • 1
    Count is the correct value :-) You're not hitting `count++` after the break. The indexing in your second for-loop is off. If you want to check this, print out the value out count after enter 5 letters, and it'll be 5. But the indices of the elements in the array are going to b 0,1,2,3,4, so you should initialise `i` as `count-1`. – splrs Jan 05 '14 at 22:17