• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't find end of file  RSS feed

 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, and that did work.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!