Reputation: 653
I am trying to condense my code as much as possible, and am trying to output my variable "Grade" in the finally instead of each case statement BUT it isn't recognizing the variable. I feel like the solution for this is way too easy but i cant figure it out for the life of me. Thank you.
Code:
public class Grade {//1
public static void main(String[] args) {//2
ConsoleReader console = new ConsoleReader(System.in);
boolean done = false;
System.out.println("Enter your grade percentage:");
do{
try{
int percent = (int) console.readDouble();
Math.round(percent);
percent = (int) percent / 10;
String grade ="Input was not valid";
if(percent <= 5){//3
grade = "Your grade is an F, Work Harder so you won't have to retake!";
System.out.println(grade);
done = true;
}else{//3//4
switch (percent){//5
case 6:
grade = "Your grade is a D, work harder";
System.out.println(grade);
done = true;
break;
case 7:
grade = "Your grade is a C, That's average but you could do better.";
System.out.println(grade);
done = true;
break;
case 8:
grade = "Your grade is a B, That is pretty good but strive for that A";
System.out.println(grade);
done = true;
break;
case 9:
grade = "Your grade is a A, Good Work!!";
System.out.println(grade);
done = true;
break;
case 10:
grade = "Your grade is a A, Good Work!!";
System.out.println(grade);
done = true;
break;
default:
grade = "Your input was invalid, Please enter your grade percentage.";
System.out.println(grade);
done = false;
break;
}//5
}//4
}catch(NumberFormatException e){
System.out.println("Please input only numbers, try again:");
}finally{
if(done == true){
//ERROR System.out.println(grade); //ERROR
System.out.println("I hope you're happy with your grade!");
}else{
}
}
}while(done == false);
}//2
}//1
Upvotes: 1
Views: 75
Reputation: 3331
You have to declare grade String outside the try - catch block. Then you can use it in finally statement.
Try this way:
String grade ="Input was not valid";
try{
int percent = (int) console.readDouble();
Math.round(percent);
percent = (int) percent / 10;
if(percent <= 5){//3
grade = "Your grade is an F, Work Harder so you won't have to retake!";
System.out.println(grade);
done = true;
}else{//3//4
switch (percent){//5
case 6:
grade = "Your grade is a D, work harder";
System.out.println(grade);
done = true;
break;
case 7:
grade = "Your grade is a C, That's average but you could do better.";
System.out.println(grade);
done = true;
break;
case 8:
grade = "Your grade is a B, That is pretty good but strive for that A";
System.out.println(grade);
done = true;
break;
case 9:
grade = "Your grade is a A, Good Work!!";
System.out.println(grade);
done = true;
break;
case 10:
grade = "Your grade is a A, Good Work!!";
System.out.println(grade);
done = true;
break;
default:
grade = "Your input was invalid, Please enter your grade percentage.";
System.out.println(grade);
done = false;
break;
}//5
}//4
}catch(NumberFormatException e){
System.out.println("Please input only numbers, try again:");
}finally{
if(done == true){
//ERROR System.out.println(grade); //ERROR
System.out.println("I hope you're happy with your grade!");
}else{
}
}
Upvotes: 0
Reputation: 4923
Declare the variable grade before the try block.
String grade ="Input was not valid";
Upvotes: 0
Reputation: 178263
You've defined grade
inside the try
block, so it's scope is limited to that of the block itself. It's not accessible outside try
, e.g. in finally
.
To make it accessible in finally
, declare grade
before the try
block, so its scope is the entire method.
Upvotes: 5