Web Analytics Made Easy -
StatCounter outFile and calculation probs - CodingForum

Announcement

Collapse
No announcement yet.

outFile and calculation probs

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • outFile and calculation probs

    Hello again all. I am back with another issue, haha. I have created a document to read from, 10 names with 5 grades each, one line per person. I need have that information come back up under my headings, added, and averaged. I think what I have should work, it compiles and executes, but not correctly. How do I get it to come up as a one line each and the average. I also need an 'A', 'B', etc... to come up with the corresponding grade. This is what I have....

    Code:
    import java.io.*;
    import java.util.*;
    
    public class Ch7Prob14
    {
    	public static void main(String[] args) throws FileNotFoundException
       {
    		//Declaring Variables		
    		String name;
    		DoubleClass courseAvg = new DoubleClass();
    		double gradeString;
    		double total;
    		int counter;
    		char calculateGrade;
    		
      		//Initializing in/outFile statements    
    		Scanner inFile = new Scanner(new FileReader("Ch7_Ex14Data.txt"));
          PrintWriter outFile = new PrintWriter("Ch7_Ex14out.txt");
    		
    		counter = 0;
    		gradeString = 0;
    		total = 0;
    		
          //Print Headings to outFile statement
          outFile.println("Student   Test1 Test2 Test3 Test4 Test5 Average Grade");
    
          //Start of Loop
    	   while (inFile.hasNext())
          {	
       		//Initializing Variables   	
    			name = inFile.next();
          	gradeString = inFile.nextDouble();    
    			counter++;
    			total = total + gradeString;
    			
    				
    			outFile.printf("%-10s \n", name);
         	   calculateAverage(inFile, outFile, courseAvg);
    			 
    		}
    
    //		outFile.print(calculateGrade);   <---This line won't compile or execute   
    	
          outFile.close();
    	}
    
       public static void calculateAverage(Scanner inFile, PrintWriter outFile,
                                           DoubleClass cAvg)
       {
    		double totalGrades = 0.0;
    		double score = 0;
    		
    		score = inFile.nextDouble();
    		
    		totalGrades = totalGrades + score;
    				
    		cAvg.setNum(totalGrades / 5);
    		
    	}
    
       public static char calculateGrade(double avg)
       {
       	if (avg >= 90)
       		return 'A';
    		
    			else if (avg >= 80 && avg <= 89)
    				return 'B';
    		
    			else if (avg >= 70 && avg <= 79)
    				return 'C';
    		
    			else if (avg >= 60 && avg <= 69)
    				return 'D';
    		
    		else 
    			return 'F';			
    		
    	}	
    }
    And this is how it comes up....

    Student Test1 Test2 Test3 Test4 Test5 Average Grade
    Johnson
    77
    Aniston
    95
    Cooper
    11
    Gupta
    30
    Blair
    96
    Clark
    45
    Kennedy
    52
    Bronson
    89
    Sunny
    28
    Smith
    49

    What is wrong with my code?
Working...
X