Sandeep
Sandeep

Reputation: 15

Continue statement not working

I am new to java

int nop=o;
BufferedReader scan = new BufferedReader( new InputStreamReader(System.in));
come_here:
System.out.println("Enter length");
try{
    int n=Integer.parseInt(scan.readLine());
nop=n;
}catch(Exception sandy){
    System.out.println("Please Enter Numericals only");
    continue come_here;
}

If user entered any string instead of numericals Exceptions occurs and prints "Please Enter Numericals only" and compiler executes next statements, here am loosing user input to overcome that I have used label (come here:), if an Exception occurs it says "Please Enter Numericals only" after that I want program to take user input again, I used continue come_here; but its not working?

any anybody tell where I have done mistake? and how to resolve that

thank you

Upvotes: 0

Views: 3351

Answers (5)

Daniel Alder
Daniel Alder

Reputation: 5372

Labels don't have the same function in Java as they have in other languages like Basic or C/C++.

They mark a loop and not a command to which you can jump to. Normally, you only need them when having nested loop. like this:

    loop_i: for (int i = 0; i < 10; i++) {
        loop_j: for (int j = 0; j < 10; j++) {
            System.out.println(i + " " + j);
            if (j == 7) {
                // we want to jump out of the inner loop
                break loop_j; // or just break;
            }
            if (i == 3 && j == 4) {
                // now we want to jump out of both loops
                break loop_i;
            }
        }
    }

The example uses break because they are easier to explain. But continue have the same rules in matters of labels.

Upvotes: 0

Akhil Thayyil
Akhil Thayyil

Reputation: 9403

Refer Continue Usage here , The usage is not like what you have learned

The following code can be used to read integer values

int nop=o;
BufferedReader scan = new BufferedReader( new InputStreamReader(System.in));
for(;;) {
        System.out.println("Enter length");
        try{
        int n=Integer.parseInt(scan.readLine());
        nop=n;
break;
            }catch(Exception sandy){
                System.out.println("Please Enter Numericals only");
            }
    }

Upvotes: 0

Ainsley H.
Ainsley H.

Reputation: 1021

I'm not saying this is the optimal way of solving this problem, but perhaps you are looking for something like this. I replaced your goto-statement with a while(true) loop. The while loop quits once the integer has successfully been parsed.

int nop=0;
BufferedReader scan = new BufferedReader( new InputStreamReader(System.in));
while (true) {
    System.out.println("Enter length");
    try {
        int n=Integer.parseInt(scan.readLine());
        nop=n;
        break;
    } catch(Exception sandy){
        System.out.println("Please Enter Numericals only");
    }
}

Upvotes: 0

Makoto
Makoto

Reputation: 106430

You're trying to do a user input loop, but the trick is that Java doesn't allow goto to actually be used - it's a reserved word, but it won't compile.

Here's what you can do in simpler steps:

Upvotes: 0

NPE
NPE

Reputation: 500317

This is not valid Java. I would instead write the code as follows:

    int nop = 0;
    BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
    for (;;) {
        System.out.println("Enter length");
        try {
            int n = Integer.parseInt(scan.readLine());
            nop = n;
            break;
        } catch (Exception sandy) {
            System.out.println("Please Enter Numericals only");
        }
    }

Upvotes: 1

Related Questions