-1

I have created this While loop to read lines from a text file. How to I stop the loop after it has found the values?

     try
    {
       BufferedReader file = new BufferedReader(new FileReader("Inventory.txt"));
       while (file.ready())
       {
          String ID = file.readLine();
          String Name = file.readLine();
          String Price = file.readLine();

          if (Value.equals(tf_ID.getText()))
          {
             System.out.println(Name +"  "+ Price);
          }
       }
    }
    catch (IOException e)
    {
       System.out.println("Error");
    }
 }
2
  • what is your programming language? PHP has a break command in the loop which can jump out and continue execution Commented Mar 14, 2015 at 10:39
  • 1
    @user2838611 do you know google? Commented Mar 14, 2015 at 10:43

2 Answers 2

2

Use keyword break, here is link with reference to branching in java:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Sign up to request clarification or add additional context in comments.

Comments

1

Make use of break; in your if statement, it will cause the while loop to stop.

if (Value.equals(tf_ID.getText()))
{
    System.out.println(Name +"  "+ Price);
    break;//your while loop will stop
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.