5 Replies Latest reply: Jan 7, 2013 11:20 AM by abillconsl RSS

    Wht this is not working ?

    981979
      Hi, 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 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 tel where i have done mistake ? and how to resolve that

      thank you
        • 1. Re: Wht this is not working ?
          rp0428
          >
          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 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 tel where i have done mistake ? and how to resolve that
          >
          The 'continue' statement can only be used in a loop; I don't see any loop in your code.

          Whenever you have a basic question about the Java language or syntax you should refer to the Java Tutorial and the Java Language Specification.

          The language spec has the rules and syntax and the tutorial has examples that show how to use features.

          For the rule for 'continue' see '14.16 The continue Statement' in the Java Language Spec
          http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf
          >
          14.16 The continue Statement

          A continue statement may occur only in a while, do, or for statement; statements
          of these three kinds are called iteration statements. Control passes to the loopcontinuation
          point of an iteration statement.

          ContinueStatement:
          continue Identifieropt ;

          A continue statement with no label attempts to transfer control to the innermost
          enclosing while, do, or for statement of the immediately enclosing method,
          constructor, or initializer; this statement, which is called the continue target, then
          immediately ends the current iteration and begins a new one.

          To be precise, such a continue statement always completes abruptly, the reason
          being a continue with no label.

          If no while, do, or for statement of the immediately enclosing method, constructor,
          or initializer contains the continue statement, a compile-time error occurs.

          A continue statement with label Identifier attempts to transfer control to the
          enclosing labeled statement (§14.7) that has the same Identifier as its label; that
          statement, which is called the continue target, then immediately ends the current
          iteration and begins a new one.
          >
          For learning how to use 'continue' see 'Branching Statements' in the tutorial
          http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
          >
          The continue Statement
          The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. The following program, ContinueDemo , steps through a String, counting the occurences of the letter "p". If the current character is not a p, the continue statement skips the rest of the loop and proceeds to the next character. If it is a "p", the program increments the letter count.


          class ContinueDemo {
          public static void main(String[] args) {

          String searchMe
          = "peter piper picked a " +
          "peck of pickled peppers";
          int max = searchMe.length();
          int numPs = 0;

          for (int i = 0; i < max; i++) {
          // interested only in p's
          if (searchMe.charAt(i) != 'p')
          continue;

          // process p's
          numPs++;
          }
          System.out.println("Found " +
          numPs + " p's in the string.");
          }
          }
          • 2. Re: Wht this is not working ?
            981979
            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 rewrite this

            thank you
            • 3. Re: Wht this is not working ?
              rp0428
              The Java Tutorial link I gave you covers all of the basics of using loops and has examples.

              Go through the tutorial and try those examples.
              • 4. Re: Wht this is not working ?
                841361
                use a while loop , the correct code should look like this:

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

                Edited by: 40044004 on Jan 7, 2013 8:29 AM
                • 5. Re: Wht this is not working ?
                  abillconsl
                  You need to add a "break" to that loop