Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 13th, 2005, 2:28 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Scanning data from a file

How can i scan the entire data from a file with undefined number of lines?

Right now, i'm using BefferdReader identifier = new BufferedReader (new FileReader(filename.format)); but that scans 1 line at a time. i want to scan the entire file.

Last edited by Dark Flare Knight; Jun 13th, 2005 at 2:30 PM.
Dark Flare Knight is offline   Reply With Quote
Old Jun 13th, 2005, 4:35 PM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
You want to read in the whole file or what?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 13th, 2005, 6:51 PM   #3
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
yes. i want to read the whole file
Dark Flare Knight is offline   Reply With Quote
Old Jun 13th, 2005, 10:39 PM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
String s="";
     String currentLine;
try{
    BufferedReader identifier = new BufferedReader (new FileReader("asd.txt"));
    for(;;){
     if((currentLine = identifier.readLine())==null)break;
     s=s+currentLine+"\n"; 
    }
    System.out.println(s);
    }catch (IOException e) {
       System.err.println("Error: " + e);
   }
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 14th, 2005, 6:32 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Why the break statement?
while ((currentLine = identifier.readLine()) != null) {
    s = s + currentLine + "\n";
}
__________________
Me :: You :: Them

Last edited by Ooble; Jun 14th, 2005 at 9:27 AM.
Ooble is offline   Reply With Quote
Old Jun 14th, 2005, 6:53 AM   #6
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Isn't that the same thing ? What are the benefits of while against for(;; ) ?
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 14th, 2005, 7:12 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
for(;; ) is faster as it doesn't have to compare anything.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 14th, 2005, 7:35 AM   #8
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
But surely you have to compare something to drop out of the loop anyway.
__________________
kirkl_uk
kirkl_uk is offline   Reply With Quote
Old Jun 14th, 2005, 9:29 AM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I believe it's generally accepted that if you can avoid the use of the break keyword, your solution will be more elegant. Your solution could have been written for ( ; (currentLine = identifier.readLine()) != null; ) - it's the same as mine.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 14th, 2005, 9:57 AM   #10
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Quote:
Originally Posted by Ooble
Why the break statement?
while ((currentLine = identifier.readLine()) != null) {
    s = s + currentLine + "\n";
}
this is the better solution
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC