Forums Register Login

Can't find end of file

+Pie Number of slices to send: Send
I'm having issues with my bottom loop trying to read in a large *.txt* file but how can I do a check to see if its at the end of the document?

1
+Pie Number of slices to send: Send
Hi Oceas, welcome to CodeRanch!

You will be more likely to attract aid if you UseCodeTags. Edit your post and try it now!

Scanner has a very nifty method called hasNext(). Do away with your for loop, and read items while input hasNext().
+Pie Number of slices to send: Send
Thank you, and that did work.
+Pie Number of slices to send: Send
 

Oceas Anderson wrote:Thank you, and that did work.


No. Get rid of the for loop. The first time in the for loop, the while loop will ensure that the whole file is read. The next 9999 through the for loop will then just confirm that there is nothing left to read from the file.

If the file may be bigger than 10000 lines and you only want to read the first 10000, then you need to modify your while loop to inclde that check - your code at the moment doesn't do that.
+Pie Number of slices to send: Send
 

Stuart A. Burkett wrote:

Oceas Anderson wrote:Thank you, and that did work.


No. Get rid of the for loop. The first time in the for loop, the while loop will ensure that the whole file is read. The next 9999 through the loop will then just confirm that there is nothing left to read from the file.

If the file may be bigger than 10000 lines and you only want to read the first 10000, then you need to modify your while loop to inclde that check - your code at the moment doesn't do that.




Corrected



And even though this works for displaying out the entire document is their a way to just simply call for the next character? I'm needing to do a encryption for each letter but plaintText.input.nextChar() doesn't seem to exist the ("nextChar()") part.
I'm really wanting to store all the characters into a array for the while. So that I can then use the hill cipher and encrypt it using the matrix input file that does work. Thanks in advanced .
+Pie Number of slices to send: Send
 

Oceas Anderson wrote:I'm needing to do a encryption for each letter but plaintText.input.nextChar() doesn't seem to exist the ("nextChar()") part.


After reading in each line as a String, just use the String.toCharArray method to get an array of chars. Or you can use the String.getCharAt method to get each character individually.
+Pie Number of slices to send: Send
 

Stephan van Hulst wrote: . . . You will be more likely to attract aid if you UseCodeTags. Edit your post and try it now! . . .

I added code tags, and removed some unnecessary balnk lines, and doesn't the code look better

Welcome again
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 679 times.
Similar Threads
Trouble with arrays / Validating user input
Problem recovering from InputMismatchException in Scanner class
Learning Project
Learning Project 2
try...catch blocks giving <identifier> expected error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:17:34.