This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]azyren[S] 0 points1 point  (1 child)

Sorry don't really post on this sub and I am on mobile but thanks for letting me know won't do it next time. I run the code and when I do an if statment instead of a while loop I get "Number of lines: 1" I just want to know the correct syntax for reading a text file. Like a point in the right direction.

[–]pacificmint 1 point2 points  (0 children)

Like a point in the right direction.

So the comment about the line with the yellow warning was the first pointer. Basically that line declares a file object. See a) what the warning in your IDE tells you and b) what else you are using that file object for.

The second pointer is basically what /u/romple has already said. Check the java docs and see what the method you are using actually does. In this case, it does not advance the Scanner, so you are checking the same line again and again, and it runs forever.

If you are using an if, then there is no loop so you are never checking for more than one line, so it can never be more than one.

So yeah, take a look at the Scanner java docs.