still having a problem

can't get a loop to read more than one item from text file

Page 1 of 1

2 Replies - 694 Views - Last Post: 06 April 2008 - 11:12 AM Rate Topic: -----

#1 whatevany  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 30-March 08

still having a problem

Post icon  Posted 06 April 2008 - 10:32 AM

Hi! I already posted once and I still can't get this to work. My program requires that we read information from a student text file and decide which students(grouped by social security number) will not be required to take final exam based on their average. My problem is that the program only reads the first grade from the first grade and calculates the average from that one grade. Someone suggested that I needed a loop to read each grade. I've tried that and it still doesn't work. Mabye the loop is in the wrong spot, I just don't know :D I'm not finished I just want to correct any mistakes before I move on. Any help would be greatly appreciated!

Here is the text file:
237-65-1253 90 91 100 80
289-12-5417 80 79 63 69
299-62-4145 81 82 89 90
435-11-4572 82 85 86 82
233-54-2641 88 96 92 89
245-88-1978 70 89 88 91

Here is the output:

----jGRASP exec: java exemptions

237-65-1253
90.0
22.5
289-12-5417
80.0
42.5
299-62-4145
81.0
62.75
435-11-4572
82.0
83.25
233-54-2641
88.0
105.25
245-88-1978
70.0
122.75

----jGRASP: operation complete.
import java.io.*;
import java.util.StringTokenizer;
import java.util.Scanner;

public class exemptions
{
	public static void main(String[] args) throws IOException
	{
		String line, fileIn="Grades.txt", fileOut="Exempt.txt";
		double grades=0;
		double average=0;
		double total=0;

		StringTokenizer tokens;
		String SSnumber;
		
		FileReader fr=new FileReader(fileIn);
		BufferedReader inFile=new BufferedReader(fr);
		
			
		FileWriter fw=new FileWriter (fileOut);
		BufferedWriter bw=new BufferedWriter(fw);
		PrintWriter outFile=new PrintWriter(bw);
		
			
		line=inFile.readLine();
		while (line!=null)
		{
			tokens=new StringTokenizer(line);
			SSnumber=null;	
			grades=0;
			average=0;
					
	
		SSnumber=tokens.nextToken();
					
				
		grades=Double.parseDouble(tokens.nextToken());//this is where I'm having the problem	
		
				
		total +=grades;
		average=total/4;
		
														
		outFile.print(SSnumber);						
		outFile.print(average);
			
		System.out.println(SSnumber);
		System.out.println(grades);
		System.out.println(average);
		
	
		line=inFile.readLine();
	}
	
		inFile.close();
		outFile.close();

		
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: still having a problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: still having a problem

Posted 06 April 2008 - 10:53 AM

Please avoid double posting.

You have 4 grades to read... you must read them

line=inFile.readLine();
while (line!=null)
{
   tokens=new StringTokenizer(line);
	
   SSnumber=tokens.nextToken();
   total = 0.0;					
   for(int i = 0; i < 4; i++) {						
	   grades=Double.parseDouble(tokens.nextToken());//this is where I'm having the problem	
	   System.out.print(" " + grades);			
	   total +=grades;
   }
   average=total/4;
   System.out.println("  Total: " + total + "  Average: " + average);								
   outFile.print(SSnumber);						
   outFile.println(average);
			
   line=inFile.readLine();
}


Was This Post Helpful? 0
  • +
  • -

#3 whatevany  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 30-March 08

Re: still having a problem

Posted 06 April 2008 - 11:12 AM

View Postpbl, on 6 Apr, 2008 - 10:53 AM, said:

Please avoid double posting.

You have 4 grades to read... you must read them

line=inFile.readLine();
while (line!=null)
{
   tokens=new StringTokenizer(line);
	
   SSnumber=tokens.nextToken();
   total = 0.0;					
   for(int i = 0; i < 4; i++) {						
	   grades=Double.parseDouble(tokens.nextToken());//this is where I'm having the problem	
	   System.out.print(" " + grades);			
	   total +=grades;
   }
   average=total/4;
   System.out.println("  Total: " + total + "  Average: " + average);								
   outFile.print(SSnumber);						
   outFile.println(average);
			
   line=inFile.readLine();
}




I screwed up, I meant to say thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1