Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 03-19-2008, 02:51 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
Interacting with files.
Currently writing a program that displays what's in a different file. What I need to do is process the data in the file so i can get three things. I need to count the number of records, and compute the mean age and GPA.

What's confusing me is how I would go about separating the file to do each thing individually.

Here's what I have so far.

Code:
/** * @(#)Lab17.java * * * @author * @version 1.00 2008/3/17 */ import java.io.*; import java.text.DecimalFormat; public class Lab17 { public static void main(String args[]) throws IOException { DecimalFormat df = new DecimalFormat("00.000"); File f1 = new File("students17.dat"); File f2 = new File("passing.dat"); File f3 = new File("honors.dat"); f2.delete(); f3.delete(); if (f1.exists()) { FileReader inFile = new FileReader(f1); BufferedReader inStream = new BufferedReader(inFile); String inString; while((inString = inStream.readLine()) != null) System.out.println(inString); int rec = Integer.parseInt(inString); int sum = 0; sum += rec; inStream.close(); System.out.println(sum); } else { System.out.println(f1.getName() + " does not exist"); } System.out.println(); } }
I get this error with the current adding I have going. I'm betting because it's trying to add letters in as well.

Code:
Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:415) at java.lang.Integer.parseInt(Integer.java:497) at Lab17.main(Lab17.java:30)

Last edited by apfroggy0408 : 03-19-2008 at 02:54 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-19-2008, 07:14 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
File f1 = new File("students17.dat"); File f2 = new File("passing.dat"); File f3 = new File("honors.dat");
Files with a dat extension are often written and read with highly specialized DataOutput/InputStreams. We generally use a txt extension for reading with a FileReader. It may work; I haven't tried.
The error on line 30 suggests that the reader read something that came up as "null". If the data in the file is only a number on each line then your read method should work.
An impertinent suggestion is to move the sum variable up above the while loop. It gets reset/declared and assigned the value of zero in each trip throught the loop.
Code:
int sum = 0; while((inString = inStream.readLine()) != null) System.out.println(inString); int rec = Integer.parseInt(inString); // int sum = 0; sum += rec;
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
Interacting with the Java Garbage Collector Java Tip Java Tips 0 03-28-2008 09:04 PM
Writing to files within jar files erhart Advanced Java 0 02-04-2008 03:50 AM
Text and image files within jar files erhart Advanced Java 8 01-19-2008 05:43 AM
how to convert mpeg files to .wav files christina New To Java 1 08-06-2007 05:14 AM
convert xls files into pdf files bbq New To Java 3 07-20-2007 04:56 AM


All times are GMT +3. The time now is 08:12 AM.


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