hi folks
the program I am attempting to do here, even though its horrible so far, is one that reads in a file containing:

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

What needs to be done is to have a value returning method for figuring out the average of each student by adding up the test score and doing the average based on that. There also need to be a void method for displaying the letter grade for each student based on the average that is figured out.
Also, I need to be able to output the information figured out to a file.
This is a sample of what it needs to look like.

Student		Test1	Test2	Test3	Test4	Test5	Average    Grade
Johnson 	  85	   83 	   77	   91 	    76
Aniston		  80 	   90 	   95 	   93	    48
…
Class Average  =

Now for my horrible code so far.....and btw I cannot use arrays so this needs to be done as simply as possible. And also, I am using a mac so my files that are being read from are kept on a USB as you will see from my input. Not sure why but things don't work with reading on my mac if kept just on the hard drive.

import java.io.*;
import java.util.*;

public class methodLab3
{
  public static void main(String[] args) throws FileNotFoundException
  {
    
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/tests.txt"));
   System.out.println("The average is: " + average(name,tests,total));
   double total;


  }
  public static double average(String name,int tests,double total) throws FileNotFoundException
  {
      Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/tests.txt"));
 
     while (inFile.hasNextLine())
     {
      name = inFile.next();
      tests = inFile.nextInt();
      total += tests;
        
    }
    return total/4;
  
}
}

  public static void displayGrade(double average) throws FileNotFoundException
  {
    if (average >= 90)
    {
      System.out.println("A");
    }
    else if (average >= 80)
    {
      System.out.println("B");
    }
    else if (average >= 70)
    {
      System.out.println("C");
    }
    else if (average >= 60)
    {
      System.out.println("D");
    }
    else
    {
      System.out.println("F");
    }
  }

Any help would be very welcome plz.

Recommended Answers

All 18 Replies

Well you either tried to trick us in solving your homework (provided code wouldn't compile) or you copy&pasted some old rubbish that doesn't work.

So what it gone be?

How about neither?
If you aren't going t be helpful and would like to throw accusations at people I suggest posting replies to a hate page or something.
Otherwise the main problem I seem to be having is with line 10 of my program in that it tells me that the variables have not been initialized but if I do in the main method and give them a value of 0 then my outcome just becomes 0 when it runs my value returning method. I suspect that my program is not separating the String name perhaps from the other 4 test scores for each student or it is just not reading the file correctly at all. I have heard of using string.split but since I never learned that yet I will not use it. I am pretty sure the code I posted is close to the answer I need with the exception of the outFile.println statement I will need to work on once I get the actual calculations.

Without trying to write Java code, can you write an outline (in English) of what your program needs to do? You can number steps like this:
1. Open the input file for reading
2. Read a line from the file
...
What needs to be done for each line in the input file?
What other processing needs to be done after you have read the input?

Coming up with an outline like this can help you to write your program one step at a time, rather than trying to write everything all at once and getting all messed up.

I was never very good at doing things like outlines whether it be for a paper or a program lol.
But here it goes:
Main method needs to:
1. Call calculateAverage method and then print to outFile.
2. Call displayGrade() to accompany the outFile averages.

calculateAverage needs to:
1. Read input from the file.
2. Separate the name of the student from the four test grades.
3. Calculate an average for each student.

displayGrade needs to:
1. Read in the calculateAverage for each student.
2. Based on the if/else decide what letter grade the student receives.

I think that just about covers it.

OK, that's a good start. Do you know how to write Java code for all of those steps? If not, pick one step that you don't know how to do and break it down further. For example, when you say calculateAverage needs to separate the name of the student from the test grades, do you know how to do that? If not, can you break it down into smaller steps? Hint: the Scanner class can be used to parse a string.

I know that you can parse with dialog boxes of javax.swing. but that's the only time i have used it. I didn't know you could do it with scanner input from a file though and I am not sure I know how to do that. I do know that I do NOT want to use string.split() to do this because I am certain there is a simple way to accomplish this.
I do have some code I altered that was from a simple program when I was first learning about files.

import java.io.*;
import java.util.*;

public class fileTester
{
public static void main(String[] args) throws FileNotFoundException
  {
    list();
  }
    public static void list() throws FileNotFoundException
    {
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/tests.txt"));
    while (inFile.hasNextInt())
    {
      int test1 = inFile.nextInt();
      int test2 = inFile.nextInt();
      int test3 = inFile.nextInt();
      int test4 = inFile.nextInt();
      double average = (test1 + test2 + test3 + test4)/4;
      System.out.println(test1 + (" ") + test2 + (" ") + test3 + (" ") + test4 + (" ") + average);
    }
    }
}

SO I know it can read the file. and yes i took the names out of the file lol. i will need to add those back but i was just trying to test that it could print out the names and the average......although it does give me an error after it lists all the numbers and averages. a java.util.NoSuchElementException error.
i also know how to calculate averages and such with input when concerning value returning methods just not with files just yet. there really isn't too much discussion or explanation in the class i am taking it is more of a free form learn the steps on your own type of class.

Good. So you can get a line from the file using

String line = infile.nextLine();

Now you can parse that line by creating a new scanner object.

Scanner parser = new Scanner(line);

Then you can get the name from the beginning of the line using parser.next() and then you can get a score from the line using parser.nextInt() .

Whoa whoa whoa......you can have more than one scanner in a file? this i have not learned yet but would of been useful to know lol

@kramerd nothing wrong with parser.nextInt() but String.split(regex) can be also useful in this scenario

String.split seems to much like an array with the [] and the input from the file being kept within {....}
There always seems to be a problem with my system.out.println in my program no matter what variation of the program i attempt. it wants the variables in average to be initialized but why would i want to do that for this.

import java.io.*;
import java.util.*;

public class methodLab3
{
  public static void main(String[] args) throws FileNotFoundException
  {
    String name;
    int tests;
    double total;
 
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/tests.txt"));
     String line = inFile.nextLine();
    Scanner parser = new Scanner(line);
    System.out.println("The averages are: " + average(name,tests,total));
    
  }
  public static double average(String name,int tests,double total) throws FileNotFoundException
  {
      Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/tests.txt"));
      String line = inFile.nextLine();
      Scanner parser = new Scanner(line);
      while (inFile.hasNextLine())
     {
      name = parser.next(); 
      tests = parser.nextInt();
      total += tests;
        
    }
    return total/4;
  
}

public static void displayGrade(double average) throws FileNotFoundException
  {
    if (average >= 90)
    {
      System.out.println("A");
    }
    else if (average >= 80)
    {
      System.out.println("B");
    }
    else if (average >= 70)
    {
      System.out.println("C");
    }
    else if (average >= 60)
    {
      System.out.println("D");
    }
    else
    {
      System.out.println("F");
    }
}
}

1. You are trying to send variables that never been initialized
2. You are not using the line that you read from file

  • Read the line
  • Forward the line to average method as parameter
  • Use second scanner or split method that returns array to get actual values from provided line
  • Do your calculation
  • Return value

So should my while loop that reads in all of the stuff in the file be done in the main method? and then include the average return value method within that while loop?

Now it works somewhat......i cut a lot of what i needed out just to check and see if it does the averages.

import java.io.*;
import java.util.*;
public class methTest
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/tests.txt"));
    
    while (inFile.hasNext())
    {
      String line = inFile.nextLine();
      Scanner parser = new Scanner(line);
    
      String name = parser.next();
      int test1 = parser.nextInt();
      int test2 = parser.nextInt();
      int test3 = parser.nextInt();
      int test4 = parser.nextInt();
      System.out.println(name + (" ") + calculateAverage(test1,test2,test3,test4));
    }
  }
  public static double calculateAverage(int test1,int test2,int test3,int test4)
  {
    return (test1 + test2 + test3 + test4)/4;
  }
}

PS : i love u guys......it gives me the answer of

Johnson 84.0
Aniston 89.0
Cooper 65.0
Gupta 68.0
Blair 50.0
Clark 57.0
Kennedy 58.0
Bronson 88.0
Sunny 71.0
Smith 70.0

i will post if i have any other issues with my program

Good work!

Ok my next problem that has arisen is one of format.

import java.io.*;
import java.util.*;
public class methTest
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/tests.txt"));
    PrintStream outFile = new PrintStream("/Volumes/SCII CE USB/testaverage.txt");
    outFile.println("STUDENT  TEST1 TEST2 TEST3 TEST4 AVERAGE GRADE");
    
    while (inFile.hasNext())
    {
      String line = inFile.nextLine();
      Scanner parser = new Scanner(line);
    
      String name = parser.next();
      int test1 = parser.nextInt();
      int test2 = parser.nextInt();
      int test3 = parser.nextInt();
      int test4 = parser.nextInt();
      outFile.println(name + (" ") + test1 + (" ") + test2 + (" ") + test3 + (" ") + test4 + (" ") + calculateAverage(test1,test2,test3,test4));
      double average = calculateAverage(test1,test2,test3,test4);
      displayGrade(average);
    }
  }
  public static double calculateAverage(int test1,int test2,int test3,int test4)
  {
    return (test1 + test2 + test3 + test4)/4;
  }
  public static void displayGrade(double average)
  {
    if (average >= 90)
    {
      System.out.println("A");
    }
    else if (average >= 80)
    {
      System.out.println("B");
    }
    else if (average >= 70)
    {
      System.out.println("C");
    }
    else if (average >= 60)
    {
      System.out.println("D");
    }
    else
    {
      System.out.println("F");
    }
  }
}

gives me

STUDENT  TEST1 TEST2 TEST3 TEST4 AVERAGE GRADE
Johnson 85 83 77 91 84.0
Aniston 80 90 95 93 89.0
Cooper 78 81 11 90 65.0
Gupta 92 83 30 69 68.0
Blair 23 45 96 38 50.0
Clark 60 85 45 39 57.0
Kennedy 77 31 52 74 58.0
Bronson 93 94 89 77 88.0
Sunny 79 85 28 93 71.0
Smith 85 72 49 75 70.0

so i guess my question is how do i get them aligned properly? and also how do i outfile the displayGrade when it cannot be added to the outfile statement i have due to the fact that it is a void method?
and also ugh sorry but how do i do a class average for this? do i initialize a count of sorts that runs in the while loop in main? like averageCount = 0; initialized and then include averageCount++; within the while loop? and then maybe include something that would sum up the averages?

Have look at this tutorial for some formatting tips

thnx for the tutorial
for the displayGrade being a part of my outFile for each student how would i do that? i know with a value returning method you are able to use a variable to represent it like a = average(test1,test2,test3,test4); but can you also possibly do that with a void method?

peter budo, I agree that split can be very useful, but when a student already knows about Scanner and probably doesn't know about regular expressions, the first option can be less intimidating.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.