0

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

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
Sandeep
  • 17
  • 1
  • 3
  • 7
    this is not what `continue` means. see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html – gefei Jan 05 '13 at 16:52
  • `continue` in Java is not the same `goto` in Basic. – MrSmith42 Jan 05 '13 at 16:53
  • How is your code even compiling with `come_here:`? – Andrew Mao Jan 05 '13 at 16:55
  • 1
    I think the big picture problem is thinking in terms of goto, rather than in terms of structured loops and conditionals. I suggest reading some Java code to get a feeling for how it works. – Patricia Shanahan Jan 05 '13 at 17:01
  • @AndrewMao Java does support labels, and "continue label;" is completely valid Java - if it's in a nested loop, for instance, you can continue to the next iteration of the outer loop via proper labeling. Sandeep appears to be misusing this, however; it's unclear what exactly is going on - the code snippet as presented shouldn't compile (would fail for me with something like "not a loop label: come_here") – James Jan 05 '13 at 21:25

5 Answers5

1

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");
        }
    }
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

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");
            }
    }
Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48
  • wat i need is if user enter any numerical value its ok it it is not means i need to tell him "Please Enter Numericals only" and repeat the code again so user enters nemerical value next time if my code was wrong, then please tell me where i have done mistake and how to resolve this thank you – Sandeep Jan 05 '13 at 17:01
0

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");
    }
}
someone
  • 6,577
  • 7
  • 37
  • 60
Pål GD
  • 1,021
  • 8
  • 25
0

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:

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

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.

Daniel Alder
  • 5,031
  • 2
  • 45
  • 55
  • 1
    It is worth noting that labels and `break – NPE Jan 05 '13 at 17:20
  • Wow.. I didn't know that and never saw any code like that. Thanks for that comment. – Daniel Alder Jan 05 '13 at 17:40
  • I don't think it's very common. I only mentioned it for completeness. – NPE Jan 05 '13 at 17:42