File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Whats wrong with my code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whats wrong with my code" Watch "Whats wrong with my code" New topic
Author

Whats wrong with my code

John Swain
Greenhorn

Joined: Feb 12, 2005
Posts: 16
hello, can some one help me out here. This code does not work, i know its got problems but im stuck. Please tell me what im doing wrong and how to fix it.

Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi John,

You know the routine! What errors do you get?

I'm not going to copy, paste and compile your code, nor am I going to look through every line carefully, but if you tell us what error you get, we will hopefully be able to glance at the code and point out the error.

So, compile error or runtime error?
Ben Poole
Ranch Hand

Joined: Jul 17, 2003
Posts: 33

That's the first problem: you seem to have some errant brackets. Don't you mean to close that code chunk rather than open another? Also, yout getName() method is void, yet you imply a return type when you say this:



Start with fixing those, and take it from there. The compiler will give you clues.
[ April 05, 2005: Message edited by: Ben Poole ]
Ricky Martaputra
Ranch Hand

Joined: Apr 04, 2005
Posts: 42
Hello John Swain,
I try to help you to explain what's wrong with your code:
1. you can't declare some other methods in a method.
2. you can't parse parameter(s) when you call a method, if the method doesn't provide it, e.g: in your getName() method, it doesn't provide parameter to be parsed, but when you called, you parsed a parameter, getName(name);
3. local variable can't be used to global, e.g in your main method, there are many global variables, and it just can be used in main method, if you want to use your local variable to another method, you can pass it in method's parameter(look at my code in getScore or getLetterGrade method). But don't forget with pass by value and pass by parameter in java.
4. look at your code : keyboard.readline(). It suppose to be keyboard.readLine(). Don't forget, java is case sensitive.
5. you forget to close the braches, e.g in if (( finalScore >= 70) && ( < 80 ))
6. if (( finalScore >= 70) && ( < 80 )), it suppose to be if (( finalScore >= 70) && ( finalScore < 80 )) and others.
7. this is not a technical issue, but in method getLetterGrade(), i always found that the final score is less than 60 if the input range between 0 - 100 .
glad to help..
cmiiw..

here's the fixed code:
import java.io.*;
public class NewGradesProgram{

private static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException{
//Section: Variables
String name;
int quiz1 = 0;
int quiz2 = 0;
double tMidTerm, tFinal, tQuiz;
int midTermExam = 0;
int finalExam = 0;
double finalScore = 0;
char grade;
name = getName();
quiz1 = getFirstQuiz();
quiz2 = getSecondQuiz();
midTermExam = getMidTermExam();
finalExam = getFinalExam();
finalScore = getScore(quiz1, quiz2, midTermExam, finalExam);
grade = getLetterGrade(finalScore);
System.out.println( "Student name: " + name );
System.out.println( "First quiz score: " + quiz1 );
System.out.println( "Second quiz score: " + quiz2 );
System.out.println( "Midterm Exam score: " + midTermExam );
System.out.println( "Final Exam score: " + finalExam );
System.out.println( "Final Score: " + finalScore );
System.out.println( "Your final grade is: " + grade );

} //End Method: main
//Method: get grades
private static String getName() throws IOException
{
System.out.println("Students calculated final scores");
System.out.println("and letter grades. ");
System.out.println("Please enter your name. ");
String name = keyboard.readLine();
return name;
}

//Method: get First Quiz
private static int getFirstQuiz() throws IOException
{
System.out.println("Please enter your first quiz grade. ");
int quiz1 = Integer.parseInt(keyboard.readLine());
return quiz1;
}
//Method: get Second Quiz
private static int getSecondQuiz() throws IOException
{
System.out.println("Please enter your second quiz grade. ");
int quiz2 = Integer.parseInt(keyboard.readLine());
return quiz2;
}
//Method: get Mid Term Exam
private static int getMidTermExam() throws IOException
{
System.out.println("Please enter your midterm exam grade. ");
int midTermExam = Integer.parseInt(keyboard.readLine());
return midTermExam;
}
//Method: get Final Exam
private static int getFinalExam() throws IOException
{
System.out.println("Please enter your finalexam grade. ");
int finalExam = Integer.parseInt(keyboard.readLine());
return finalExam;
}
//Method: Calculate Score
private static double getScore(int quiz1, int quiz2, int midTermExam, int finalExam)
{
double tQuiz = (((quiz1 + quiz2)/20)*.25);
double tMidTerm = ((midTermExam/100)*.25);
double tFinal = ((finalExam/100)*.50);
double finalScore = tQuiz + tMidTerm + tFinal;
return finalScore;

}
//Method: get letter grade
private static char getLetterGrade(double finalScore){
char grade = 'E';//assumption:if grade < 60 then grade E
if ( finalScore >= 90 )
{
grade = 'A';
}
else if (( finalScore >= 80) && ( finalScore < 90 ))
{
grade = 'B';
}
else if (( finalScore >= 70) && ( finalScore < 80 ))
{
grade = 'C';
}
else if (( finalScore >= 60) && ( finalScore < 70 ))
{
grade = 'D';
}
return grade;
}
}//End Class: gradesProgram
John Swain
Greenhorn

Joined: Feb 12, 2005
Posts: 16
Thanks for the help.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by John Swain:
hello, can some one help me out here. This code does not work, i know its got problems but im stuck. Please tell me what im doing wrong and how to fix it.


Well, you could sit down with your Java book and a compiler and really work on the mechanics of the language. Get so you really understand how methods and their arguments work, how braces format and structure your code, how variables and types work... that sort of thing. Tackle small programs before taking on big ones. Force yourself to never write a single line of code that you couldn't explain to someone.

Or, you could just keep posting until someone eventually does your homework for you. That works too.

- Jeff
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
"Or, you could just keep posting until someone eventually does your homework for you. That works too."

Apparently that works pretty well, judging from Ricky's reply. Of course I haven't actually looked at or run Ricky's code so who knows.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whats wrong with my code
 
Similar Threads
trying to make ths work
Grading Program..... (part 2)
Grading Program..... (part 2)
Grading Program--Need help, please!!!
grade program