The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Grade Calculator

8 replies on 1 page. Most recent reply: Mar 22, 2004 9:37 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Java Grade Calculator Posted: Mar 19, 2004 11:46 PM
Reply to this message Reply
Advertisement
Im having trouble coding a java grade calculator using JOptionPane i have to have a JOptionPane in which you can enter grades such as 88-100 and it has to transform them to letter equivlents and out to a JOptionPane i also need to code a try/catch block for any grade over 100 or incase a alapha value is entered for error catching. I m having trouble basically setting up the while loop and setting the variables

A = 88-100 // not sure how to code the variable to be equal to more then one number hence 88, 89 , 90

B = 80-87

C = 67-79

D = 60-67

F = <60

Need to create a string object to control the while loop aswell

heres my code so far

public class Program2gc {
public static void main(String[] args) {
int grade1 = 88 - 100, grade2 = 80 - 87, grade3 = 67 - 79, grade4 = 60 - 67,
grade5 = 0 - 60; // not sure if this part is correct
String inputString = JOptionPane.showInputDialog(null, "Enter Grade: ",
"Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
String message = ("Numeric Grade is: " + inputString +
"\nYour Letter Grade Is: " + something + "\n\n"
+ "To Continue Press Enter. \n"
+ "To exit Press 'x': ");
JOptionPane.showInputDialog(null, message, "Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
double something = 0;

System.exit(0);
}

Any Help is Appreciated

Thank you,

Meth


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Java Grade Calculator Posted: Mar 20, 2004 9:15 AM
Reply to this message Reply
I won't do your homework for you, but I will help you figure out what you are doing wrong. You are correct that the following code is incorrect. You cannot assign multiple values to a variable. Get rid of this line entirely.
int grade1 = 88 - 100, grade2 = 80 - 87, grade3 = 67 - 79, grade4 = 60 - 67, grade5 = 0 - 60; // not sure if this part is correct

INPUT - PROCESS - OUTPUT You need to do things in that order. The first line below allows the data to be input. The second outputs data. But you haven't processed the data in between.
String inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
 
//You need code between these two
 
String message = ("Numeric Grade is: " + inputString + "\nYour Letter Grade Is: " + something + "\n\n" + "To Continue Press Enter. \n"
+ "To exit Press 'x': ");


BTW, as you will see later, you don't want to declare the variables on the same line as you get the data. This is usually a better way to handle things.
String inputString;
//other code
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);

Here's what I think that you need to do. I'm sure others might do a few thing differently, but there are multiple ways to solve problems.
1. You need four variables. In addition to the two Strings that you already have, you need something to store the grade in numerical form and something to store the letter grade in.

For the numerical grade, any of the numerical primitives will work, but int is the most likely choice.

For the letter grade, you could use either a char or a String. You don't have to give this variable a value yet.

Declare all of your variables at the beginning of your program BEFORE you do anything else. You do not have to give the Strings any values yet (see example above), but you should give the grade variable an invalid value like -1.

2. Start a loop that checks to see if the numerical grade variable is outside the accepted range. You want to keep the loop going while the variables value is wrong! Put what follows in the body of the loop. This is why you have to declare the variables first. If you don't, they won't be available outside of the loop.

3. Start a try block.

4. In the try block, let the user enter the data. DON'T redeclare your variable!!!!
//wrong
String inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
//right
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);


3. Use the appropriate wrapper class to convert your String data to numerical data and store that value in your grade variable.

4. Still inside the try block, use an if statement to determine if the grade is inside or outside the acceptable parameters. I'd use the showMessageDialog() method to send a message to the user saying that they entered an invalid number, and asking them to please try again. I would not use the showInputDialog() method to actually try to get the data again yet. Let the loop take care of that.

5. Create the catch block. In it I would probably reset the grade variable to the invalid value to force the loop to continue. Then use the showMessageDialog() method to display a message indicating that the user entered non-numeric data.

6. Close the loop after the end of the catch block. When your code gets this far, you will either have a valid number in the grade variable, which will end the loop, or you will still have an invalid value (like -1), so the loop will repeat and ask the user to enter a grade.

7. After the loop is finished, use a series of if-else statements to determine what range the numerical grade falls in. Set the letter grade in the various blocks. Be sure to use else on the last choice so the letter grade variable get set to something.
if(A condition)
  letterGrade = "A";
else if(B condition)
//other code
else
  letterGrade = "F";

A switch would also work, but I like to avoid the switch statement.



8. Modify your output code to include the letter grade variable and you should be good to go.

I hope this helps.
twc

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Grade Calculator Posted: Mar 20, 2004 9:14 PM
Reply to this message Reply
Ty alot for your help so far heres is what i have and still not getting anywhere



public class Program2gc {
public static void main(String[] args) {
int grades = -1;
char letter;
int something;
int something2;
String inputString;
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
String message = ("Numeric Grade is: " + inputString +
"\nYour Letter Grade Is: " + "\n\n" +
"To Continue Press Enter. \n"
+ "To exit Press 'x': ");

while (!(inputString > 0 || inputString < 100)) { //not sure if the while is right
try {
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ","Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
if (grades < 0 || grades > 100)
JOptionPane.showInputDialog(null, message, "Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
String message2 = ("Invalid Entry:" + inputString + "\n\n" +
"Enter Valid Grade Again:");
}
catch (NullPointerException e) {
}
}
System.exit(0);
}
}


what you layed out so far has helped heaps i just cant get the jist of the while loop and where to go with it.

alos only one joptionpane is comming up and then the program is ending not sure why


Thx

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Java Grade Calculator Posted: Mar 20, 2004 11:50 PM
Reply to this message Reply
I've highlighted a few problem areas.

//You have this in two places, you don't need the one outside the loop.
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
 
//Two problems here.  First of all, inputString is a String, so it can't store numbers.
//What does store numbers is grade!
while (!(inputString > 0 || inputString < 100)) { //not sure if the while is right
 
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ","Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
//You skipped step 3 here.  You must convert the data from a String to an int so it can  
//be stored in grade.  The Integer class has a method to help you do that.
if (grades < 0 || grades > 100)
 
//I think you want this to display message2, but you have to create message2 FIRST.
//But you need to use a block with your if for that to happen.
JOptionPane.showInputDialog(null, message, "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
String message2 = ("Invalid Entry:" + inputString + "\n\n" + "Enter Valid Grade Again:");


You still haven't done steps 5 through 8. That is over half the program.

BTW, what do you plan to use something and something2 for?

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Grade Calculator Posted: Mar 21, 2004 1:33 AM
Reply to this message Reply
I figured i didnt need the somethings so did away with them this is how far i am as of now still not doing what it should alos i forgot if a negative number/number over 100 or chracter is entered need to have an invalid message displayed





public class Program2gc {
public static void main(String[] args) {
double grade = 0;
String inputString = "";
while(!(inputString.equalsIgnoreCase("x"))){
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
grade = Double.parseDouble(inputString);
if (grade <= 100 || grade >= 88)
inputString = JOptionPane.showInputDialog(null,"Grade..." + inputString +
"\n" + "Letter Grade... \tA",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
else if (grade <= 87 || grade >= 80)
inputString = JOptionPane.showInputDialog(null,"Grade..." + inputString +
"\n" + "Letter Grade... \tB",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
else if (grade <= 79 || grade >= 67)
inputString = JOptionPane.showInputDialog(null,"Grade..." + inputString +
"\n" + "Letter Grade... \tC",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
else if (grade <= 67 || grade >= 60)
inputString = JOptionPane.showInputDialog(null,"Grade..." + inputString +
"\n" + "Letter Grade... \tD",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
else if (grade <= 60 || grade >= 0)
inputString = JOptionPane.showInputDialog(null,"Grade..." + inputString +
"\n" + "Letter Grade... \tF",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);

try {
}

catch (NullPointerException e) {
}
}


Thx
Meth

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Grade Calculator Posted: Mar 21, 2004 4:29 AM
Reply to this message Reply
Heres my current code im gonna call it quits for tonight i think it looks alot better and im closer but im still having problems. What its not doing is when ya enter a different grade it keeps outputting A for the letter everytyme also the last else i coded keeps saying im missing an if and the last Inputdialog which is only supposed to catch errors keeps showiong up in the loop


public class Program2gc {
public static void main(String[] args) {
double grade = 0;
String inputString = "";
String lettergrade = "";
while(!(inputString.equalsIgnoreCase("x"))){
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
if (grade <= 100 || grade >= 88)
lettergrade = "A";
else if(grade <= 87 || grade >= 80)
lettergrade = "B";
else if (grade <= 79 || grade >= 67)
lettergrade = "C";
else if (grade <= 67 || grade >= 60)
lettergrade = "D";
else if (grade <= 60 || grade >= 0)
lettergrade = "F";
inputString = JOptionPane.showInputDialog(null,
"Grade..." + inputString +
"\n" + "Letter Grade..." + lettergrade
+ "\n\n" + "Press 'x' To Exit..." +
"\nEnter To Continue..",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
else (grade < 0 || grade > 100
JOptionPane.showInputDialog(null, "Entry Invalid..." +
"\n\n" + "Press 'x' To Exit..." +
"\nOr Enter To Try Again...",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
try{
grade = Double.parseDouble(inputString);
}
catch (NumberFormatException e) {
System.exit(0);
}
catch (NullPointerException e) {
System.exit(0);
}

}
System.exit(0);
}
}


Ty

Meth

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Grade Calculator Posted: Mar 21, 2004 4:58 PM
Reply to this message Reply
I got it all coded now except i keep getting a nullpointerexception when i press cancel on the 2n input dialog only problem all done heres my almost finished code

public class Program2gc {
public static void main(String[] args) {
double grade = 0;
String inputString = "";
String lettergrade = "";
while(!(inputString.equalsIgnoreCase("x"))){
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
try {
grade = Double.parseDouble(inputString);
if (grade <= 100 && grade >= 88) {
lettergrade = "A";
}
else if(grade <= 87 && grade >= 80) {
lettergrade = "B";
}
else if (grade <= 79 && grade >= 67) {
lettergrade = "C";
}
else if (grade <= 67 && grade >= 60) {
lettergrade = "D";
}
else if (grade <= 60 && grade > 0) {
lettergrade = "F";
}
else if (grade < 0 || grade > 100 ) {
lettergrade = "Numerical Value Was Invalid";
}
inputString = JOptionPane.showInputDialog(null,
"Entered Grade..." + inputString +
"\n" + "Letter Grade..." + lettergrade
+ "\n\n" + "Press 'x' To Exit..." +
"\nEnter To Continue..",
"Benjamin Shively",
JOptionPane.PLAIN_MESSAGE);
}
catch (NumberFormatException e) {
grade = 0;
inputString = JOptionPane.showInputDialog(null,"ERROR..." + "\n\n" +
"Non Numerical Entry..."
+ "\n\n" +
"Press 'x' To Exit..." +
"\nEnter To Continue..",
"Benjamin Shively",
JOptionPane.ERROR_MESSAGE);
}
catch (NullPointerException e) {
System.exit(0);
}
}
}
}

thx for the help so far

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Java Grade Calculator Posted: Mar 22, 2004 9:33 AM
Reply to this message Reply
I'm glad to see that you came up with your own solution instead of using mine. That is a better learning experience anyway.

When the user presses "Enter", a String is created and assigned to inputString. Even if the Textfield is left blank, an empty String ("") is created. On the other hand, when "Cancel" is pressed, no String is created and inputString is considered null - a term that means that the name inputString isn't assigned to any object at this time.

 inputString.equalsIgnoreCase("x")

If inputString is null, then there isn't any object to call the equalsIgnoreCase() method. That is what NullPointerException is all about. However, since the while loop conditional is outside of the try block, the catch block you created won't help you. You can eliminate it.

One question needs to be answered first. If the user presses "Cancel", what do you want to have happen? Since I can't ask you directly, I am going to guess that you want "Cancel" and "Enter" to be treated the same way. If you want "Cancel" to be treated the same as "x" then you will need to modify what I do.

You could check to see if inputString is null and reset it to the desired equivalent. This code would have to go AFTER you ask the questions, but BEFORE the end of the loop. Change "" to "x" if you want "Cancel" and "x" to behave the same way.
if(inputString == null)
   inputString = "";


Another option is to check for null in the while conditional.
while ((inputString == null) || //rest of your code)


The third option is not very common, but it does work. Change the while conditional to call the method from the String literal.
while(! "x".equalsIgnoreCase(inputString))


This last one really works, but you wouldn't see it in a classroom setting. It might not be the best choice in this situation. BTW, don't use all of these, just one!

Matt Gerrans

Posts: 1152
Nickname: matt
Registered: Feb, 2002

Re: Java Grade Calculator Posted: Mar 22, 2004 9:37 PM
Reply to this message Reply
Hey Meth, If you use the java tags when posting, your code will look a lot better and maintain its indentation. See the Gray box to the right when you are composing the post.

Flat View: This topic has 8 replies on 1 page
Topic: networking Previous Topic   Next Topic Topic: MouseListener freezing up the repaint()


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us