Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Help reading File

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a .txt file with some sentences typed in in this format:
Sentence 1
Sentence 2
Sentence 3

Sentence 4
Sentence 5

I can read and show the contents of the file but what the problem is I just want it to show sentences 1 through 3. What do i use for it to recognize the new line? Ive tried the Search but to no avail.
-Clay
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you doing currently to read the file and display it? If you are only using a FileReader, it only allows you to read a single character at a time. However, you can use a BufferedReader to wrap around it and then read a whole line at a time. If you don't know how to do this (or even if you do), I strongly suggest you the I/O trail from the Java Tutorial. This should get you up to speed on the various classes that are available for reading and writing files.

Layne
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay heres my code:


What i want to do is when there is a blank line to have it stop reading. Ive tried if(line=="\n") but it doesnt work. Anyone know the correct way?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Display line.trim().length() of each line inside your loop and see if you can find a way for the program to spot the blank one.
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much. Works now!
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic