0

so, i'm learning about Java JOptionPane, and i want to make another question that refer to the question before that and get the input from the question, but it seem like i can't get the variable right ??

int papamoney = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog(null, "Do your father work?", null, papamoney);

if (papamoney == JOptionPane.YES_OPTION){
    String x = (String)JOptionPane.showInputDialog(null, "How much is your father salary", "Type salary in rupiah");
}
System.out.println ("Your dad salary per month is " +  x);

and here it seems like the string x from the question is un-used while the compiler say that variable x cannot be found?

sorry if my english is not perfect

Mikheil Zhghenti
  • 734
  • 8
  • 28
  • Look up variable scope, and then declare the String variable *before* the if-block, not *inside* the if-block where it is only visible within that block. – DontKnowMuchBut Getting Better Oct 17 '20 at 21:21
  • Variables declared inside a block will not be accessible outside that block. In your case, `String x` is defined inside the `if(){ ... }` block, so the variable will not be accessible outside. You can define `String x` before the `if` block, and assign the value within the `if(){ ... }` block. – Vishnu Haridas Oct 19 '20 at 10:19

0 Answers0