break statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    break statement

    Good Day all!...Could I ask one question about break please...
    Why I am getting nothing when I put this break? at least it prints something
    if it's true...please refer my codes below:
    Code:
    if(name2.equals(name1)) {
           if(add2.equals(add1))  {
              if(!(val2.equals(val1)))              {                                                                                                 
       printWriter.println(fileno + " : " + name2 );
        break;
                         }                    
                    } 
                     
               }
    It works fine if I take out the break statement. But then it will exercise all lines in my inputfile. What I want is, after I got the first output then it will stop checking the next line....please feel free to reply me. thank you.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    Good Day all!...Could I ask one question about break please...
    Why I am getting nothing when I put this break? at least it prints something
    if it's true...please refer my codes below:
    Code:
    if(name2.equals(name1)) {
    if(add2.equals(add1)) {
    if(!(val2.equals(val1))) { 
    printWriter.println(fileno + " : " + name2 );
    break;
    } 
    } 
     
    }
    It works fine if I take out the break statement. But then it will exercise all lines in my inputfile. What I want is, after I got the first output then it will stop checking the next line....please feel free to reply me. thank you.
    Post code for the whole loop that you are trying to break from.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      break statements can only occur in loop bodies where it stops the loop immediately
      or in case clauses in which case the break prevents "fall-through" and breaks
      out of the entire switch statement. Your code snippet doesn't show loops nor
      switch statements ...

      kind regards,

      Jos

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #4
        Originally posted by JosAH
        break statements can only occur in loop bodies where it stops the loop immediately
        or in case clauses in which case the break prevents "fall-through" and breaks
        out of the entire switch statement. Your code snippet doesn't show loops nor
        switch statements ...

        kind regards,

        Jos
        Thank you very much..Now I know about the break..I thought it can stop from execute/read next line (FileReader) by putting the 'break' statement.
        so there's no way to put break after 'if' syntax..

        One more question about readLine() please...
        How am I going to get the last line (EOF)- value to be printed?
        I have this code:

        Code:
        line1 = in1.readLine();
        line2 = in2.readLine();
        
        while ((line1 ! = null) && (line2 != null)) {
         ............ 
        ....
        line1 = in1.readLine();
        line2 = in2.readLine();
        }
        
        //then I need to print out the last line for both files?
        Kindly please help me what's the syntax for printing the lastline value?
        Thank you

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          try this code ......

          while((line1 = in1.readLine()) != null && (line2 = in2.readLine()) != null)
          {
          //prints two strings here.
          }

          if(line1 != null)
          {
          //prints the line1
          while((line1 = in1.readLine()) != null) //prints the line1
          }
          else if(line2 != null) //prints the line2
          {
          //prints the line2
          while((line2 = in2.readLine()) != null) //prints the line2
          }
          i think u got my logic.
          have a good day.

          kind regards.
          dmjpro.

          Comment

          • shana07
            Contributor
            • Jan 2007
            • 280

            #6
            Originally posted by dmjpro
            try this code ......



            i think u got my logic.
            have a good day.

            kind regards.
            dmjpro.
            sorry friend, I don't really get you...anyway I have tried to follow your code - but it doesn't print the last line value still...thank you.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by shana07
              Thank you very much..Now I know about the break..I thought it can stop from execute/read next line (FileReader) by putting the 'break' statement.
              so there's no way to put break after 'if' syntax..

              One more question about readLine() please...
              How am I going to get the last line (EOF)- value to be printed?
              I have this code:

              Code:
              line1 = in1.readLine();
              line2 = in2.readLine();
               
              while ((line1 ! = null) && (line2 != null)) {
              ............ 
              ....
              line1 = in1.readLine();
              line2 = in2.readLine();
              }
               
              //then I need to print out the last line for both files?
              Kindly please help me what's the syntax for printing the lastline value?
              Thank you
              Code:
               String lastLine = ""; 
              while(line != null) {
               
              line = input.readLine();
              if(line != null) { 
              	 lastLine = line;
              }
              }
               
              System.out.println(lastLine);

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                hello shana07

                did u understand my // line??????

                do one more thing if u can plz post ur code what u write with the help of me....

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  My 2 cents: Don't use break/continue to manover your way around loops. Bad form old boy.

                  Unless you have some profiling proof that says that you should use it, don't. It makes for harder to read and verify the code.


                  Adrian

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    good idea suggested by AdrianH.

                    if there is no way then u should use it ... otherwise not.

                    Comment

                    Working...