aspose file tools*
The moose likes Beginning Java and the fly likes NEWBIE NEEDS HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NEWBIE NEEDS HELP" Watch "NEWBIE NEEDS HELP" New topic
Author

NEWBIE NEEDS HELP

Anthony Vit
Greenhorn

Joined: Nov 27, 2004
Posts: 7
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

Joined: Oct 04, 2004
Posts: 218
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

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Anthony Vit
Greenhorn

Joined: Nov 27, 2004
Posts: 7
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

Joined: Oct 02, 2003
Posts: 11499
    
  16

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Anthony Vit
Greenhorn

Joined: Nov 27, 2004
Posts: 7
Thanks Fred and Layne!! Fred that extra line was the problem. I didn't even realize it.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NEWBIE NEEDS HELP