0

The input file that I am trying to read contains the amount of lines for a block of entries on the first line, and records on the subsequent lines. The file may contains many blocks like this. For example:

3
a b c
a b 
a c b //Three in the first line tells that there are 3 subsequent lines. 
2
a b
a b c
0 //the input ends here

I am trying to use a while loop to read one block at a time, process it, and then read in another block. However the code works only when there is one block. When there are multiple blocks, it freezes.. Can someone help to figure out what is wrong? Thank you!

public static void main(String[] args) {
    Scanner s = new Scanner (System.in);

    while (s.hasNextLine()){
        String number = s.nextLine();
        if (line.equals("0")){
            break;
        }else {

            int n = Integer.parseInt(number);
            for (int i=0; i<n; i++){
                 String letters= s.nextLine();
                 ...
                //do something to store the lines of letters 
            }
        }                   
    }   
}
6
  • 2
    if (!line.equals("0")) Shouldn't that be if (line.equals("0"))? Commented Sep 25, 2014 at 6:58
  • Write out in plain language what that if statement does. Commented Sep 25, 2014 at 6:58
  • Even the if condition is not required it hasNextLine will read till the end of file Isn't Commented Sep 25, 2014 at 7:00
  • It is important to mention in your code example how the scanner is used in the for loop - I hope you have not forgotten to do something like s.nextLine() to actualy advance in the file. Commented Sep 25, 2014 at 7:03
  • Thanks. yes it should be if (line.equals("0")). I edited the question. But it still doesn't work.. Commented Sep 25, 2014 at 7:27

4 Answers 4

2

The best way to get out of a while loop (other than using the control conditions carefully), is to use a break statement.

System.exit(0) will shut down the Java Virtual Machine and return the status code 0 back to the operating system. That's a little drastic. And status code 0 is conventionally used to indicate success: so your return value is idiosyncratic.

That all said, your immediate problem is a typographical one, drop ! from the if:

if (line.equals("0")){

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

Comments

2

Others have pointed out that you missed a ! when checking the value of line, but there's a perhaps even bigger problem with that line: you don't ever assign anything to line. In fact, as things stand, it shouldn't even compile, unless line is a class field. (But even then, the check is no use unless the value of line might alter throughout the loop execution.)

7 Comments

Thanks. Yea I missed out a line when I typed up the code here. I edited the question. But it still doesn't work for input of more than one block of information..
You've added an assignment, but it's still outside the loop, so the value of line won't change as the loop progresses. In other words, if the first line of the file is "0" then the program will exit straight away, and if the first line isn't "0" then that System.exit(0) call will never get made.
I think I put the assignment inside the for loop and the outer while loop? Sorry I don't quite get what you mean.
In your code as displayed in your question, you assign to line once, outside both loops. You assign to letters inside both loops, but that's a different variable. In fact what you really want to do is exit the outer loop based on the value of letters, and ignore line completely.
The line variable isn't magic: it doesn't automatically update when the next line of the for comes in unless you have some code starting with line=.... In your case, whenever you read the next line, you update letters, not line.
|
0

Both while and for loops can be stopped using the break statement.

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

Comments

0

Try this code:

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);

    while(s.hasNext()) {

        int a = Integer.parseInt(s.nextLine());

        if(a == 0) 

            break;

        else {

            for(int i = 0; i < a; i++)

            System.out.println(s.nextLine());
        }

    }
}

You used System.exit(0) instead of break.

System.exit(0) stops the Java Virtual Machine execution from loop, not break. So use break to exit the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.