Results 1 to 2 of 2
  1. #1
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default 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.

    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();
        	
       
        	
        }
        
        
    }
    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:
    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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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.
    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

  1. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:04 PM
  2. Writing to files within jar files
    By erhart in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 02:50 AM
  3. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 AM
  4. how to convert mpeg files to .wav files
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:14 AM
  5. convert xls files into pdf files
    By bbq in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •