Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 06:14 AM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
Help with filereader in java
I'm writing a program now that takes a ID and 3 numbers fom a text file each in a line such as "23AFS 98 34.3 99" on each line, seperated by a space each. Im using a scanner and filereader to get the information from the file but Im not sure as to how I can set these numbers so that I can get the average, min value, max value etc.
Here is the code for the filereader.

Code:
try { FileReader reader = new FileReader("scores.txt"); Scanner in = new Scanner(reader); while (in.hasNext()) { String line = in.next(); } } catch (IOException exception) { System.out.println("Error procession file: " + exception); }
If anymore information is needed, just ask. Mainly I just need to set it up as each ID in a variable and each number in a variable, I think thats what I have to do but I can't figure it out exactly, and is this the best way to do it? Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2007, 01:06 AM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
take the contents of the line and load it into a stringtokenizer. Iterate through the stringtokenizer and pull the values out.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-26-2007, 11:55 AM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
Here this is an example of what you need to do:
Code:
BufferedReader reader = null; try { //Generate a file reader to read the file FileReader fileReader = new FileReader("YourFile.txt"); //Use a BufferedReader to allow each line to be read one at a time. reader = new BufferedReader(fileReader); String line = null; //Load the next line and check the results were not null Note the ()'s while(null != (line = reader.readLine())) { //Take advantage of the split method to split each item out. String[] items = line.split(" "); String idString = items[0]; long id = Long.parseLong(idString); //Repeat with other numbers using the wrapper objects parse methods. } } catch(Exception e) { //TODO Implement proper error handling. e.printStackTrace(); } finally { try { reader.close(); } catch(Exception e) { e.printStackTrace(); } }
Beware though I have not tested this code or compiled it so treat it thus.

Hopefully its pretty self explainatory but if you need a bit more help let me know.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by shanePreater : 07-26-2007 at 11:57 AM. Reason: Missed the finally!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
FileReader Vs FileInputStream and same goes to output unhurt New To Java 2 11-01-2007 03:29 PM


All times are GMT +3. The time now is 09:58 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org