4 Replies - 9474 Views - Last Post: 21 February 2009 - 09:30 AM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Problem with test scores and grades program

Post icon  Posted 20 February 2009 - 05:38 PM

I have a problem. I made a program that asks the user to enter 3 scores. The program returns the average of the 3 scores and shows the user what grade was earned from what score. I get 8 error messages. What am I doing wrong? Here is the code:

import java.util.Scanner;

/*
	PROGRAM: TestScores.java
	Written by Julio C. Villa
	Write a program that has variables to hold 3 test scores.  The program should ask the user to enter 
		three test scores and then assign the values entered to the variables.  The program should display 
		the average of the test scores and the letter grade that is assigned for the test score average.  
		Use the grading scheme in the following table:  
*/

public class TestScores
{
	public static void main(String[ ] args)
	  {

			   Scanner keyboard = new Scanner(System.in); 

				
		System.out.print("Enter the first score");
				String score1;
				score1 = keyboard.nextLine();

				System.out.print("Enter the second score");
				String score2;
				score2 = keyboard.nextLine();

				System.out.print("Enter the third score");
				String score3;
				score3 = keyboard.nextLine();

				String answer;
				answer = (score1 + score2 + score3 / 3);

				if (answer >= 90)
			 
				System.out.println("The average score is + answer + .  You earned an A.");

				else if (answer >= 80 && answer <= 89)

				System.out.println("The average score is + answer + .  You earned a B.");

				else if (answer >= 70 && answer <= 79)

				System.out.println("The average score is + answer + .  You scored a C.");

				else if (answer >= 60 && answer <= 69)

				System.out.println("The average score is + answer + .  You scored a D.");

				else 

				System.out.println("The average score is + answer + .  You scored an F.");
	}
}


Thank you for your help!

Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with test scores and grades program

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1137
  • View blog
  • Posts: 7,099
  • Joined: 07-September 06

Re: Problem with test scores and grades program

Posted 20 February 2009 - 05:43 PM

You can't add strings together, unless you want to get another string back.

You will have to change the strings to doubles or ints to allow you to use mathmatical operations on them.
Was This Post Helpful? 0
  • +
  • -

#3 Delta62  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 19-February 09

Re: Problem with test scores and grades program

Posted 20 February 2009 - 06:52 PM

String answer;
answer = (score1 + score2 + score3 / 3);


If you add String objects together such as 5, 4, and 9, you will get 549 as a result. In addition, 549 is not an int, it is still a String.
You can use the Integer wrapper class instead:
int answer;
answer = (Integer.parseInt(score1) + Integer.parseInt(score2) + Integer.parseInt(score3) / 3);


That will return the value that you want.
Was This Post Helpful? 0
  • +
  • -

#4 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Problem with test scores and grades program

Posted 21 February 2009 - 08:25 AM

That helped me greatly, thank you. Now that I changed the strings to int's now I get 3 error messages and all point out to keyboard.nextline(); is this supposed to be different? Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#5 toxicaze  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 06-February 09

Re: Problem with test scores and grades program

Posted 21 February 2009 - 09:30 AM

The solution to your problem is to replace "keyboard.nextLine();" with "keyboard.nextInt();" since you are no longer reading from a String variable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1