-1

I'm fairly new to coding and I've been learning by creating simple programs.

I'm trying to create a program called roundGrade to round a grade to one decimal place by calling onto the command line.

The error stated:

Error: variable roundGrade might not have been initialized

Here's the code I've written so far:

public static String roundGrade(double grade){

    String roundGrade;
    double R = Double.parseDouble(roundGrade);
    R = Math.round(grade*10)/10;
    roundGrade = Double.toString(R);

    return roundGrade;
}
Hamid Rouhani
  • 2,309
  • 2
  • 31
  • 45

1 Answers1

1

You are attempting to parse roundGrade before you set it to anything (and for no apparent purpose). This

double R = Double.parseDouble(roundGrade);
R = Math.round(grade*10)/10;

should be something like

double R = Math.round(grade*10)/10;

And your entire method could be

return String.format("%.1f", grade);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249