• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NEWBIE NEEDS HELP

 
Anthony Vit
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. This is my 1st post and I want to thank everyone in advance for any help that you can give me. 1st off this programming this is already not for me, but here is my question.
I have to write a program that reads a students name together with his or her test scores. The program should then compute the average test score for each student and assign the app. letter grade. This is what my input looks like:
Balto85 83 77 91 76
Mickey80 90 95 93 48
Minnie78 81 11 90 73
Doc92 83 30 69 87
Goofy23 45 96 38 59
Duckey60 85 45 39 67
Grumpy27 31 52 74 83
Sunny93 94 89 77 97
Piggy79 85 2893 82
Pluto85 72 49 75 63

I am having a problem with the loop. I am lost. I can get the output for the 1st student, but that is where it ends. I also need to give a class average, but I didn't even get that far yet. Can someone please help.
Here is what I have so far:

import java.util.*;
import java.io.*;
import java.text.DecimalFormat;

public class Grade5
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

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

double test1, test2, test3, test4, test5;
double average;
String firstName;
String grade;
StringTokenizer st;

BufferedReader inFile = new
BufferedReader(new FileReader("C:\\test1.txt"));

PrintWriter outFile = new
PrintWriter(new FileWriter("C:\\testavg.out"));

DecimalFormat twoDecimal =
new DecimalFormat("0.00");

st = new StringTokenizer (inFile.readLine());
firstName = st.nextToken();


test1 = Double.parseDouble(st.nextToken());
test2 = Double.parseDouble(st.nextToken());
test3 = Double.parseDouble(st.nextToken());
test4 = Double.parseDouble(st.nextToken());
test5 = Double.parseDouble(st.nextToken());


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


if(average >= 90)
grade = "A";
else if(average >= 80)
grade = "B";
else if(average >= 70)
grade = "C";
else if(average >= 60)
grade = "D";
else
grade = "F";

outFile.println("Name" + " " + "Test1" + " " + "Test2" + " " + "Test3" + " " + "Test4" + " " + "Test5" + " " + "Average" + " " + "Grade");
outFile.println(firstName + " " + test1 + " " + test2 + " " + test3 + " " + test4 + " " + test5 + " " +
twoDecimal.format(average) + " " + grade);
outFile.println();

outFile.close();
}
}


Thanks again in advance!!!
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony,

Welcome to javaranch.

Looking at your code I feel you will find a for loop beneficial.
Some example code using a for loop with if statements:


Hope the above helps.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic issue is that you need to put everything from
st = new StringTokenizer (inFile.readLine());
to
outFile.println();
in a loop

I would make it a while loop.
 
Anthony Vit
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all of your help. I put all of that stuff in, but now my problem is that I am only outputting the even number of names. I have 10 input names and the output file is only containing names 2,4,6,8, and 10??? Any thoughts??? Thanks again for the help!!!

HERE IS THE CODE

import java.util.*;
import java.io.*;
import java.text.DecimalFormat;

public class Grade7
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

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

double test1, test2, test3, test4, test5;
double average;
String firstName;
String grade;
StringTokenizer st;

BufferedReader inFile = new
BufferedReader(new FileReader("C:\\test1.txt"));

PrintWriter outFile = new
PrintWriter(new FileWriter("C:\\testavg.out"));

DecimalFormat twoDecimal =
new DecimalFormat("0.00");
outFile.println("Name" + "\t\t" + "Test1" + "\t" + "Test2" + "\t" + "Test3" + "\t" + "Test4" + "\t" + "Test5" + "\t" + "Average" + "\t" + " " + "Grade");
String myLine = inFile.readLine();

while ( myLine != null )
{
st = new StringTokenizer( myLine );

st = new StringTokenizer (inFile.readLine());
firstName = st.nextToken();


test1 = Double.parseDouble(st.nextToken());
test2 = Double.parseDouble(st.nextToken());
test3 = Double.parseDouble(st.nextToken());
test4 = Double.parseDouble(st.nextToken());
test5 = Double.parseDouble(st.nextToken());


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


if(average >= 90.0)
grade = "A";
else if(average >= 80.0)
grade = "B";
else if(average >= 70.0)
grade = "C";
else if(average >= 60.0)
grade = "D";
else
grade = "F";

outFile.println(firstName + " \t\t" + test1 + "\t" + test2 + "\t" + test3 + "\t" + test4 + "\t" + test5 + "\t" + twoDecimal.format(average) + "\t" + " " + grade);
myLine = inFile.readLine();
}
outFile.close();

}
}


HERE IS THE INPUT FILE:

Balto85 83 77 91 76
Mickey80 90 95 93 48
Minnie78 81 11 90 73
Doc92 83 30 69 87
Goofy23 45 96 38 59
Duckey60 85 45 39 67
Grumpy27 31 52 74 83
Sunny93 94 89 77 97
Piggy79 85 2893 82
Pluto85 72 49 75 63

HERE IS MY OUTPUT FILE:

NameTest1Test2Test3Test4Test5Average Grade
Mickey 80.090.095.093.048.081.20 B
Doc 92.083.030.069.087.072.20 C
Duckey 60.085.045.039.067.059.20 F
Sunny 93.094.089.077.097.090.00 A
Pluto 85.072.049.075.063.068.80 D

 
fred rosenberger
lowercase baba
Bartender
Posts: 12144
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony,

please use the "code" tags when you post your source - it will preserve the indenting. there's a button below the "add reply" button that will pop then into your post, then just paste your formatted code between them.

Now, as to only outputting the even numbered lines... here's an abrideged version of your code:



do you see how within this single loop, you are calling readLine twice? once line is being fed to the tokenizer, and the next line is being given to myLine. i'm guessing you probably want to take out the line i marked with "this one", but i haven't really dug into your code too much...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To help find the problem, you should step through the program by hand. You can make a table of the variables in your program and pretend like you are the computer, stepping through the program one line at a time. Any time the value of a variable changes, write down the new value in the table and cross out its old value. This process should give you an idea of where the problems in your program are.

Also, you can add System.out.println() commands to your program to print out the values of the variable at crucial parts of the code. You can double-check to make sure these values are the same as the ones you calculated by hand.

HTH

Layne
 
Anthony Vit
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred and Layne!! Fred that extra line was the problem. I didn't even realize it.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're in JDK 5 take a look at Scanner. There are some cool options to pull numbers and strings out of a stream. It would be interesting to see an instructor's reaction ... if he's expecting you to write this the hard way and you find something in the JDK to do all the work, are you clever or lazy?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic