-3

hi i have issue with the following program as it gives me an error says that variable gradePoints might not have been initialized, what am i missing here..? also how can i improve the code so that it accept letters a+ as A+ and any other input throw an error? Thanks!!

   import java.lang.*;
import java.io.*;
import java.util.*;
import java.util.Scanner;

  public class Program
 {
 static final Map<String, Double> gradeToPointMap =
        new LinkedHashMap<String, Double>() {{
            put("A+", 4.0);
            put("A", 4.0);
            put("A-", 3.7);
            put("B+", 3.3);
            put("B", 3.0);
            put("B-", 2.7);
            put("C+", 2.3);
            put("C", 2.0);
            put("C-", 1.7);
            put("F", 0.0);
        }};

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter A Letter Grade: ");
    String letterGrade = keyboard.next();

    getGradePoint(letterGrade);


}
     public double getGradePoint(String letterGrade)
     {
         Double gradePoints = gradeToPointMap.get(letterGrade.toUpperCase());
    if (gradePoints == null)
        System.out.println("Unknown letter grade " + letterGrade);
    else
        System.out.println("Your grade point(GPA) is " + gradePoints);
     }

 }
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
divya88nair
  • 15
  • 1
  • 7
  • For string comparison see http://stackoverflow.com/questions/9870985/if-condition-does-not-work/9870998#9870998 among many others – hmjd Nov 08 '12 at 09:59
  • 1
    Why the downvotes ? It's a perfectly reasonable beginners question ? – Minion91 Nov 08 '12 at 10:10
  • Why did you edit and change your question completely? You pasted the code that Peter Lawrey answered with, now what? Do you have a new problem with **that** code? – maba Nov 08 '12 at 10:38
  • the method should be static type .Write public static double getGradePoints(...... – Wayne Rooney Nov 08 '12 at 10:40

7 Answers7

1

Initialize gradePoints at the beginning like:

double gradePoints = 0.0;

Or add this to your last else block

gradePoints = 0.0;

Also use letterGrade.equals("A+") for comparing the values because == operator will compare references and not string content.

UPDATE:

What the problem is that when user enters wrong information then your gradePoints doesn't get initialized as it gets initialized in other if-else blocks. So when at the end you try to use in println then the compiler complains variable gradePoints might not have been initialized

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
1

Member variables (class variables) gets implicitly initialized with default value during Object creation. This doesn't happen to local variable of a method since the Object doesn't know/see the local variable. You, thus, have to explicitly initialize it if you want to use the local variable.

When declaring a local variable, assign a default value.

double gradePoints;

Must be:

double gradePoints = 0.0;

When doing String comparison (by value), one must use the String.equals() or String.equalsIgnoreCase() method. The == is used for object comparison and not value comparison.

E.g.: if( letterGrade == "F") must be written as if( letterGrade.equals("F")).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • Deserves Up vote !!! it gives me an error says that variable gradePoints might not have been initialized ? This is the perfect answer to this question. – neel.1708 Nov 08 '12 at 11:18
1

For your interest, this is how I would write it.

static final Map<String, Double> gradeToPointMap =
        new LinkedHashMap<String, Double>() {{
            put("A+", 4.0);
            put("A", 4.0);
            put("A-", 3.67);
            put("B+", 3.33);
            put("B", 3.0);
            put("B-", 2.67);
            put("C+", 2.33);
            put("C", 2.0);
            put("F", 0.0);
        }};

public static void main(String... args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter a letter grade as one of " + gradeToPointMap.keySet());
    String letterGrade = keyboard.next();

    Double gradePoints = gradeToPointMap.get(letterGrade.toUpperCase());
    if (gradePoints == null)
        System.out.println("Unknown letter grade " + letterGrade);
    else
        System.out.println("Your grade point(GPA) is " + gradePoints);
}

it gives me an error says that variable gradePoints might not have been initialized, what am i missing here..?

{
    System.out.println("Error, you did not enter the correct information");
    // gradePoints is not set to anything here
}

also how can i improve the code so that it accept letters a+ as A+ and any other input throw an error?

Don't use == for String instead you want to use .equals()

Except in your case you want .equalsIgnoreCase

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Change

double gradePoints;

to

double gradePoints = 0.0;

For the accepting a+, A+, etc. use this:

if (letterGrade.equalsIgnoreCase("A+") { ... }
tckmn
  • 57,719
  • 27
  • 114
  • 156
0

initiate gradePoints

double gradePoints = 0.0 ;

Also use equals to check string

if( letterGrade.equals("A+"))
  {
      gradePoints = 4.0;
  }

do same for others.

To check for valid String do like

  String grades = "A+A-B+B-C+C-D"

  letterGrade = keyboard.next();

   if(grades .indexOf(letterGrade )){
      // process your logic
   }else{
      System.out.println("Enter valid grade ");
    }
someone
  • 6,577
  • 7
  • 37
  • 60
0

you should put a default value for this variable or in the else block. Because when

  else
  {
    System.out.println("Error, you did not enter the correct information");
  }

occurs, your variable has not been initialized, and you use it afterwards.

btw: use String.equals() rather than string1 == string2

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

@divya88nair : You shouldn't accept the answer just because somebody gave you a solution in platter (Compelete code ) Instead go for the answer which added something to your knowledge and guided towards the solution.

About you question ,

1) Method variable have to be initialised before use.
2) You may not intialise instance variable because instance variable are intialsed to their default value when you create a object.
3) == is used for refrence comparison in case of objects.
4) equals is used to check if two objects are meaningfully equal and equalsIgnoreCase is used to check same ignoring the case.

@Peter Lawrey : I got nothing against you but i think there are some basic things that programmer should know and these are very basic.

" Why the downvotes ? It's a perfectly reasonable beginners question ? " , Yes it is resonable to ask such question but dont just blindly pick up the answer , try to understand what need to be done and try to write code on your own you may not write as efficient code as some the SO expert might give you but you will get there if you try it yourself and go on improving it further, This is just a suggesion based on my personal experience.

neel.1708
  • 315
  • 1
  • 4
  • 18