java variable not initialized

2019-10-07 01:43发布

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);
     }

 }

标签: java
7条回答
ら.Afraid
2楼-- · 2019-10-07 02:13

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 ");
    }
查看更多
疯言疯语
3楼-- · 2019-10-07 02:17

@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.

查看更多
\"骚年 ilove
4楼-- · 2019-10-07 02:19

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

查看更多
▲ chillily
5楼-- · 2019-10-07 02:31

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")).

查看更多
唯我独甜
6楼-- · 2019-10-07 02:34

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

查看更多
小情绪 Triste *
7楼-- · 2019-10-07 02:35

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

查看更多
登录 后发表回答