• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't find end of file

 
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, and that did work.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic