Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I wanted to ask how do we write this program to calculate the average test score of a 5 student

·Input: file containing 5 student’s first name, last name, five test scores

·Output: file containing 5student’s first name, last name, five test scores, average of five test scores


I have try it but all i get is errors..

I'm using NetBeans IDE 7.1 program..

------------------------------------------------------------------------------------------------

EDIT: Let's bring the code up here, it's more readable here:

Java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class Homework {

    public static void main(String[] args)throws FileNotFoundException    {   

        Scanner inFile = new Scanner(new FileReader("FiveTestScores.txt")); 

        PrintWriter outFile = new PrintWriter("FiveTestScoresAverage.out");

            String firstname;            
            String lastname;

            int test1, test2, test3, test4, test5;            
            double average;

            firstname = inFile.next(); // get the first name

            lastname = inFile.next();  // get the last name

            test1= inFile.nextInt();             
            test2= inFile.nextInt(); 
            test3= inFile.nextInt();             
            test4= inFile.nextInt(); 
            test5= inFile.nextInt(); 

           System.out.println("Student Name: " + firstname + " " + lastname);

           System.out.printf("Test scores: %2d %2d %2d " + "%2d %2d ", test1, test2, test3, test4, test5);

           average = (test1 + test2 + test3 + test4 + test5) /5.00;

           System.out.printf("\nAverage of test scores:%5.2f" , average);

           inFile.close();    	    
           outFile.close();    
   }
}


if i do this steps 5 times it become an error..I want to why is that..

and how I could do it for 5 student.....

also it says that i can't write inFile.nextInt(); a lot of times... Why is that??

What can I do??
Posted
Updated 13-Mar-12 3:40am
v5
Comments
Prasad_Kulkarni 13-Mar-12 2:15am    
can you post your code which you have tried..
TorstenH. 13-Mar-12 4:04am    
I added a homework tag - cause that's what it is.
Please ask specific questions to your code.

We will help, but we do not do homework.
Richard MacCutchan 13-Mar-12 6:01am    
Show the code and, more importantly, the errors.
MysteriousAmber 13-Mar-12 9:09am    
This is the right code..package homework; import java.util.*;

import java.io.*;public class Homework {

public static void main(String[] args)throws FileNotFoundException {

Scanner inFile = new Scanner(new FileReader("FiveTestScores.txt"));

PrintWriter outFile = new PrintWriter("FiveTestScoresAverage.out");

String firstname; String lastname;

int test1, test2, test3, test4, test5; double average;

firstname = inFile.next(); // get the first name

lastname = inFile.next(); // get the last name

test1= inFile.nextInt(); test2= inFile.nextInt();

test3= inFile.nextInt(); test4= inFile.nextInt();

test5= inFile.nextInt();

System.out.println("Student Name: " + firstname + " " + lastname);

System.out.printf("Test scores: %2d %2d %2d " + "%2d %2d ", test1, test2, test3, test4, test5);

average = (test1 + test2 + test3 + test4 + test5) /5.00;



System.out.printf("\nAverage of test scores:%5.2f" , average);

inFile.close(); outFile.close(); }}

if i do this steps 5 times it become an error..I want to why is that..

and how I could do it for 5 student.....

also it says that i can't write inFile.nextInt(); a lot of times... Why is that??

What can I do??

1 solution

OK, these are questions. +5 for that.

- you should probably use loops on this task, it literally demands for them:
reading students until no more students are available -> while loop
reading 5test scores for a student -> for loop

- figuring the average value is a top task for an own method with a return value. You should divide the sum with the number of test scores - that's more valid. the result will be a double value even if the sum is given as a int.
 
Share this answer
 
Comments
MysteriousAmber 13-Mar-12 10:28am    
Thanks a lot ^__^
MysteriousAmber 13-Mar-12 13:53pm    
what does this mean??

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at homework.HomeWork.main(HomeWork.java:30)
Java Result: 1
TorstenH. 13-Mar-12 15:26pm    
you have a missmatch in the input - the int values are probably not OK.
solution:
Read value as String and convert by using Integer.parseInt(String). That one throws a NumberFormatException when the value is not ok (I don't know if you're already into Exceptions, but the Netbeans IDE will help you on this task.).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900