I am working on a homework assignment. The assigntment info is as follows:

Write a grading program that helps to determine your letter grade based on your exam score. For example, when you put in exam score of 80, the program will determine you get a B. Please note the following:

1) Your letter will be determined by only one exam score.

2) The program will keep on prompting you to put more scores until you hit the E button on your keyboard.

3) The scoring standard:
A 90 – 100
B 80 – 89
C 70 – 79
D 60 – 69
F Below 60

Here is my coding:

import java.util.Scanner; // needed for Scanner class
public class LetterGrade {
	
	public static void main(String[] args){
		
		String userInput; // To hold the user's input
		int letterGrade; // The user's letter grade
		boolean moreGrade = true;
		
		while (moreGrade){
			System.out.print("Please enter a score or press E to exit the program ");
			
			// create Scanner object for keyboard entry
			Scanner keyboard = new Scanner(System.in);
			userInput = keyboard.nextLine();
			letterGrade = userInput.charAt(0);
			
			if ((letterGrade >= 90) & (letterGrade <= 100)) {
				System.out.println("The letter grade is A");							
			} else if ((letterGrade >= 80) & (letterGrade < 90)){
				System.out.println("The letter grade is B");
			} else if ((letterGrade >= 70) & (letterGrade < 80)) {
				System.out.println("The letter grade is C");
			} else if ((letterGrade >= 60) & (letterGrade < 70)) {
				System.out.println("The letter grade is D");
			} else if ((letterGrade >= 0) & (letterGrade < 60)) {
				System.out.println("The letter grade is F");
			} else if (letterGrade == 'E' || letterGrade == 'e'){
				System.out.println("Thank you for using the grading system");
				moreGrade = false;				
			} else {
				System.out.println("That was an invalid entry, please try again");
			}
		}
	}
}

I am having a couple of issues here.
1) No matter what score I enter it always prints "The letter grade is F."

2) When I enter "E" it prints "The letter grade is D", but if I enter "e" the program exits.

Here is what it looks like when I run the program:

Please enter a score or press E to exit the program 98
The letter grade is F
Please enter a score or press E to exit the program 88
The letter grade is F
Please enter a score or press E to exit the program 77
The letter grade is F
Please enter a score or press E to exit the program 65
The letter grade is F
Please enter a score or press E to exit the program 54
The letter grade is F
Please enter a score or press E to exit the program E
The letter grade is D
Please enter a score or press E to exit the program e
Thank you for using the grading system

Try printing out the value of letterGrade after it is assigned a value to see what the computer thinks it is.

The data type on your variable seems wrong to me. The input should be an integer (i.e. the number grade). The way you do your scanner input would also change along with the data type.
Good luck.

Ok, so I have changed my if statements to look like this:

if ((letterGrade >= 90) & (letterGrade <= 100)) {
				System.out.println("The letter grade for " + letterGrade + " is A");							
			} else if ((letterGrade >= 80) & (letterGrade < 90)){
				System.out.println("The letter grade for " + letterGrade + " is B");
			} else if ((letterGrade >= 70) & (letterGrade < 80)) {
				System.out.println("The letter grade for " + letterGrade + " is C");
			} else if ((letterGrade >= 60) & (letterGrade < 70)) {
				System.out.println("The letter grade for " + letterGrade + " is D");
			} else if ((letterGrade >= 0) & (letterGrade < 60)) {
				System.out.println("The letter grade for " + letterGrade + " is F");
			} else if (letterGrade == 'E' || letterGrade == 'e'){
				System.out.println("Thank you for using the grading system");
				moreGrade = false;				
			} else {
				System.out.println("That was an invalid entry, please try again");
			}

This is what the output now looks like:

Please enter a score or press E to exit the program 97
The letter grade for 57 is F
Please enter a score or press E to exit the program 86
The letter grade for 56 is F
Please enter a score or press E to exit the program 79
The letter grade for 55 is F
Please enter a score or press E to exit the program 69
The letter grade for 54 is F
Please enter a score or press E to exit the program 55
The letter grade for 53 is F
Please enter a score or press E to exit the program E
The letter grade for 69 is D
Please enter a score or press E to exit the program e
Thank you for using the grading system

So I atleast know that my if statements are working correctly, but the wrong information is being assigned to letterGrade. Looks like I will have to try figure out what is causing letterGrade to receive the wrong values. Thanks for the help!!

Maybe I'm misinterpreting the problem, but don't you want the user's input to be a number? If so, change the userInput variable to an int, and use that as the test in the if statements. The scanner will use the function nextInt();
Good luck, you're getting there.

The values of letterGrade that you printed are the ASCII code values for the first digit of the number that you typed in. The char value returned by charAt is promoted to int values in your code.
For example: '9' = 57, '8' = 56 'E' = 69 and so on.

To get the numeric value for the Strings that you are entering you must use a conversion method such as Integer.parseInt().
Be sure to test for letter input before trying to convert the input String to numbers.

Hi.

You have to add a new method "Leer.java" (is in spanish).

//LEER. (lee dato en consola)
import java.io.*;
public class Leer{
	public static String dato(){
		String sdato = "";
		try{
		// Definir un flujo de caracteres de entrada: flujoE
			InputStreamReader isr =
				new InputStreamReader(System.in);
			BufferedReader flujoE=
				new BufferedReader(isr);
		// Leer. La entrada finaliza al pulsar la tecla Entrar
			sdato = flujoE.readLine();
		}
		catch(IOException e){
			System.err.println("Error: " + e.getMessage());
		}
		return sdato; // devolver el dato tecleado
	}

	public static short datoShort(){
		try{
			return Short.parseShort(dato());
		}
		catch(NumberFormatException e){
			// valor más pequeño
			return Short.MIN_VALUE;
		}
	}

	public static int datoInt(){
		try{
			return Integer.parseInt(dato());
		}
		catch(NumberFormatException e){
			// valor más pequeño
			return Integer.MIN_VALUE;
		}
	}

	public static long datoLong(){
		try{
			return Long.parseLong(dato());
		}
		catch(NumberFormatException e){
			// valor más pequeño
			return Long.MIN_VALUE;
		}
	}

	public static float datoFloat(){
		try{
			Float f = new Float(dato());
			return f.floatValue();
		}
		catch(NumberFormatException e){
			// No es un Número; valor float.
			return Float.NaN;
	}
	}

	public static double datoDouble(){
		try{
			Double d = new Double(dato());
			return d.doubleValue();
		}
		catch(NumberFormatException e){
			// No es un Número; valor double.
			return Double.NaN;
		}
	}

	public static String datoString(){
		return dato();
	}
	
	public static char datoChar(){
		int c=0;
		try{
			InputStreamReader isr =
				new InputStreamReader(System.in);
			BufferedReader flujoE=
				new BufferedReader(isr);
			c=flujoE.read();
			char car;
			car=(char) c;
			return car;
		}
		catch(IOException e){
			return '\0';
		}
	}
}
//Fin LEER

And change your code.

import java.util.Scanner; // needed for Scanner class
public class LetterGrade {
	public static void main(String[] args){
		String userInput; // To hold the user's input
		int letterGrade; // The user's letter grade
		boolean moreGrade = true;
			while (moreGrade){
				System.out.print("Please enter a score or press E to exit the program ");
				// create Scanner object for keyboard entry
//HERE!!! Leer.datoInt():
				letterGrade=Leer.datoInt();
					if ((letterGrade >= 90) & (letterGrade <= 100)) {
						
							System.out.println("The letter grade is A");
								} else if ((letterGrade >= 80) & (letterGrade < 90)){
									System.out.println("The letter grade is B");
								} else if ((letterGrade >= 70) & (letterGrade < 80)) {
									System.out.println("The letter grade is C");
								} else if ((letterGrade >= 60) & (letterGrade < 70)) {
									System.out.println("The letter grade is D");
								} else if ((letterGrade >= 0) & (letterGrade < 60)) {
									System.out.println("The letter grade is F");
									System.out.println(letterGrade);
								} else if (letterGrade == 'E' || letterGrade == 'e'){
									
									System.out.println("Thank you for using the grading system");
									moreGrade = false;
							} else {
							System.out.println("That was an invalid entry, please try again");
						}
			}
	}
}

I hope it helps!

Byegon...

@Leon
Please use code tags when posting code.

What is the purpose of all the code you posted? Why do you think your code is better than the Scanner class?

Are you trying to confuse the OP by posting this stuff?

commented: I was thinking the same thing +2

Ok, so now I have changed my scanner to look like this:

// create Scanner object for keyboard entry
			Scanner keyboard = new Scanner(System.in);
			letterGrade = keyboard.nextInt();

All of the letter grades are now showing correcty when I enter a score. Now my only problem is trying to get the exit part to work when E or e is entered. I am getting an exception when I try to enter E or e. I am sure it has to something with the fact that letterGrade is designated to be an int, not a string. I am just not sure how I am suppose to get it to accept the user to enter E or e to exit. Also, if I enter a number less than 0 or 102 or higher it tells me "That was an invalid entry, please try again" like it is suppose to, but if I enter 101 the program exits.

This is the exception I am getting when I use E or e:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at LetterGrade.main(LetterGrade.java:15)

Hey, just trying to help from one student to another.

If this is confusing the OP..., I`m sorry, I didn`t realize he was getting help from Bill Gates....

@Leon The purpose here is to help students LEARN how to program. Giving them large confusing undocumented programs doesn't help.

@New2Java2010
If you expect both numeric and character input you must read the input as a String.
To get the numeric value for the Strings that you are entering you must use a conversion method such as Integer.parseInt().
Be sure to test for letter input(the "E") before trying to convert the input String to numbers.

The purpose of "Leer" is to read...
And i don´t know if is better, but it works.

@ NormR1
I tried using the parseInt like you said, but I don't think I am using it correctly, because using "E" or "e" to exit still doesn't work. I am now getting this exception:

Exception in thread "main" java.lang.NumberFormatException: For input string: "e"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at LetterGrade.main(LetterGrade.java:20)

This is what my while statement looks like now:

while (moreGrade){
			System.out.print("Please enter a score or press E to exit the program ");
			
			// create Scanner object for keyboard entry
			Scanner keyboard = new Scanner(System.in);
			letterGrade = keyboard.nextLine();
			
			if (letterGrade == "E" || letterGrade == "e"){
				System.out.println("Thank you for using the grading system");
				moreGrade = false;			
			} else if ((Integer.parseInt(letterGrade) >= 90) & (Integer.parseInt(letterGrade) <= 100)) {
				System.out.println("The letter grade for " + letterGrade + " is A");							
			} else if ((Integer.parseInt(letterGrade) >= 80) & (Integer.parseInt(letterGrade) < 90)){
				System.out.println("The letter grade for " + letterGrade + " is B");
			} else if ((Integer.parseInt(letterGrade) >= 70) & (Integer.parseInt(letterGrade) < 80)) {
				System.out.println("The letter grade for " + letterGrade + " is C");
			} else if ((Integer.parseInt(letterGrade) >= 60) & (Integer.parseInt(letterGrade) < 70)) {
				System.out.println("The letter grade for " + letterGrade + " is D");
			} else if ((Integer.parseInt(letterGrade) >= 0) & (Integer.parseInt(letterGrade) < 60)) {
				System.out.println("The letter grade for " + letterGrade + " is F");
			} else {
				System.out.println("That was an invalid entry, please try again");
			}
		}

The grade part still works fine, just not the exit part.

Use the equals() method to test String object's contents, not the == operator. Read the String's API doc there are other methods that are also useful for you.

How about instead of using E to exit you use an integer like -1. This way you don't have to mess around with strings and parsing integers and equals methods. Why make things harder than they need to be? ;)

Here you go...this should get you started. As NormR1 mentioned above, you were taking in grades and converting them, automatically, to the ASCII code which would make them always below 60. You needed to take the keyboard input and user the numberical Int value only:

import java.util.Scanner; // needed for Scanner class
public class LetterGrade {
	
	public static void main(String[] args){
		
		String userInput; // To hold the user's input
		int letterGrade; // The user's letter grade
		boolean moreGrade = true;
		
		while (moreGrade)
		{
			System.out.print("Please enter a score or press E to exit the program ");
			
			// create Scanner object for keyboard entry
			Scanner keyboard = new Scanner(System.in);
			letterGrade = keyboard.nextInt();
			
			if ((letterGrade >= 90) && (letterGrade <= 100))
			{
				System.out.println("The letter grade is A");							
			} 
			else if ((letterGrade >= 80) && (letterGrade < 90))
			{
				System.out.println("The letter grade is B");
			} 
			else if ((letterGrade >= 70) && (letterGrade < 80)) 
			{
				System.out.println("The letter grade is C");
			} 
			else if ((letterGrade >= 60) && (letterGrade < 70)) 
			{
				System.out.println("The letter grade is D");
			} 
			else if ((letterGrade >= 0) && (letterGrade < 60))
			{
				System.out.println("The letter grade is F");
			} 
			else if (letterGrade == 'E' || letterGrade == 'e'){
				System.out.println("Thank you for using the grading system");
				moreGrade = false;				
			} 
			else 
			{
				System.out.println("That was an invalid entry, please try again");
			}
		}
	}
}

@PDB1982
What happens with your code when the user enters the single letter: E

@ Grn Xtrm
I would loved to be able to use something other than E, but part of the assignment said that I had to use E to exit the program.

@ NormR1
The equals() method worked great. Thanks for all of the help. Everything with the program now works great. Thanks!!!

Glad to help.

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.