Results 1 to 2 of 2
Thread: Interacting with files.
- 03-19-2008, 01:51 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
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.
I get this error with the current adding I have going. I'm betting because it's trying to add letters in as well.Java 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(); } }
Java 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 01:54 AM.
- 03-19-2008, 06:14 AM #2
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.Java Code:File f1 = new File("students17.dat"); File f2 = new File("passing.dat"); File f3 = new File("honors.dat");
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.
Java Code:int sum = 0; while((inString = inStream.readLine()) != null) System.out.println(inString); int rec = Integer.parseInt(inString); // int sum = 0; sum += rec;
Similar Threads
-
Interacting with the Java Garbage Collector
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:04 PM -
Writing to files within jar files
By erhart in forum Advanced JavaReplies: 0Last Post: 02-04-2008, 02:50 AM -
Text and image files within jar files
By erhart in forum Advanced JavaReplies: 8Last Post: 01-19-2008, 04:43 AM -
how to convert mpeg files to .wav files
By christina in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:14 AM -
convert xls files into pdf files
By bbq in forum New To JavaReplies: 3Last Post: 07-20-2007, 03:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks