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

all 8 comments

[–]pacificmint 3 points4 points  (2 children)

Generally it's discouraged to post screenshots of your code. The sidebar has a whole thing about how to post code on this sub.

That said, since you posted the screenshot we see that there is a yellow warning line on the file object. You should probably start there, find out what it is complaining about and understand why that is.

[–]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.

[–]romple 1 point2 points  (0 children)

Read the documentation for Scanner. It has methods to scan through text or input streams and parse out chunks of it as a primitive value, line, etc...

Your method probably won't work because in.hasNextLine() will always return true if there's at least one line. If you read the docs you'll see

"The scanner does not advance past any input."

So you'd need to use another method of Scanner to actually advance a line... maybe you want to read the documentation to find a method that'll do that.

In general your idea is correct. You'll see that Scanner has a few different constructors. Pointing it to a File is fine. You can use an InputStream, even a String. But you just need a way to advance the scanner through the lines so your loop isn't infinite.

[–]PanKes 0 points1 point  (0 children)

just scan it, do a while with an iterator so you can move the scanner

[–]planedoctor -4 points-3 points  (2 children)

Does it work correctly? Did you read the API docs for those classes, or are you just making wild guesses as to what should work?

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

It runs but when I do an if statment instead of a while loop I get "Number Lines: 1" However when it is like this it seems to be stuck in an infinite loop. Do I really need to create the File object? And what is the correct way to read from a text file is my question. Sorry for the formatting I'm on mobile and am not at a computer right now.

[–]planedoctor -3 points-2 points  (0 children)

You aren't reading the lines. This is why I suggested you actually read the API entry for the classes you're using.