I have the folowing code, but there is a compiling error of:

MathProblemRevised.java:25: error: cannot find symbol
                  if (inputCharacter.equals("Y")) {
                      ^
  symbol:   variable inputCharacter
  location: class MathProblemRevised
1 error

With the code:

import static java.lang.System.*;
import java.util.Scanner;

class MathProblemRevised {
   public static void main(String args[]) {

      Scanner keybardInput = new Scanner(System.in);
      Scanner keybardInput2 = new Scanner(System.in);

      System.out.println(" What is 5 + 12?");
      System.out.print(" ");
      double inputNumber = keybardInput.nextDouble();
      System.out.println();
      if (inputNumber == 17){
         System.out.println(" You are correct!");
      }
            if (inputNumber != 17){
               System.out.println(" You are incorrect!");
               System.out.println(" Do you want to give it another try? Y/N");
               //String inputCharacter;
               System.out.print(" ");
               String inputCharacter = keybardInput2.next();
               }

                  if (inputCharacter.equals("Y")) {
                  String inputCharacter = keybardInput2.next();

      } else {
         out.println(" Thanks for taking your time!");
       }
   }
}

How wold I fix this problem?

by declaring inputCharacter outside of that if statement.

the compiler will check all possibilities of that code, one is: the if statement will not be executed, hence, the inputCharacter variable is never declared, yet you do try to use it.

Sorry stultuske, this is nothing to do with what code gets executed.
In short, the scope of a declared variable is everywhere between the closest pair of curly braces
The variable declared on line 22 is in scope from line 17 to line 23
So it's out of scope on line 25

OK. I've tried moving things around, but still didn't the program to compile. Could anyone give me some code fixes or at least hints of what to do?

JamesCherrill: I guess I choose my words badly. I meant "out of scope", but just saying that usually beginning developers don't understand.

I meant to say that as far as the compiler and JVM can see, the variable doesn't exist, I just tried to explain it in a less technical way, which, indeed, when looked at as a technical explanation, is far from correct.

Alright, I got the program to compile and work as I want it to work, with the code below:

import static java.lang.System.*;
import java.util.Scanner;

class MathProblemRevised {
   public static void main(String args[]) {

      Scanner keybardInput = new Scanner(System.in);
      Scanner keybardInput2 = new Scanner(System.in);

      System.out.println(" What is 5 + 12?");
      System.out.print(" ");
      double inputNumber = keybardInput.nextDouble();


      System.out.println();
      if (inputNumber == 17){
         System.out.println(" You are correct!");
      }
            else {
               System.out.println(" You are incorrect!");
               System.out.println(" Do you want to give it another try? Y/N");
               }

               System.out.print(" ");
               String inputCharacter = keybardInput2.next();

                  if (inputCharacter.equals("Y")) {
                     System.out.print(" ");
                     inputCharacter = keybardInput2.next();

      } else {
         out.println(" Thanks for taking your time!");
       }
   }
}

Now, I'm facing with a different issue. What if the user types 'y' instead of capital "Y". How do I make the program still work. I've tried

if (inputCharacter.equals("Y" || "y"))

but this won't let me compile. Is there a way to make this work without writing another if statement?

you could do two things:

if ( inputCharacter.equals("y") || inputCharacter.equals("Y"))
is more like your current code, but the String class itself provides with an alternative in the form of the equalsIgnoreCase method.

if ( inputCharacter.equalsIgnoreCase("y"))

will return true for both "y" and "Y"

I've found the solution! I replaced

if (inputCharacter.equals("Y"))

with

if (inputCharacter.equalsIgnoreCase("Y"))

and the program works well!

have you considered making inputCharacter an actual char? in use of resources, it would be a bit more efficiƫnt (of course, on a small piece of code like this, it won't matter, but still), and we know it'll never have more than one char as value.

Thanks for the explanation stultuske.

How would I make a loop with my program to get it running untill the user either quits (by not chosing to input "y") or gets the right answer?

String input = "y";
while( input.equalsIgnoreCase("y")){
// run
input = readNewValueForInput();
}

should do the trick, but there are other ways too.

OK, the program works as I wanted it to work from the very beginning. I've finished it a week ago and decided to share it with those who need a basic outline for creating a math program.

import static java.lang.System.*;
import java.util.Scanner;

class MathProblemRevised {

   public static void main(String args[]) {
      double inputNumber;
      String inputCharacter;

      Scanner keybardInput = new Scanner(System.in); 
      System.out.print(" What is 5 + 12?" + '\n' + " ");
      inputNumber = keybardInput.nextDouble();
      System.out.println();

      if (inputNumber == 17){
         System.out.println(" You are correct!");
      }
         else {
            System.out.println(" You are incorrect!");
            System.out.print(" Do you want to give it another try? Y/N" + '\n' + " ");
            inputCharacter = keybardInput.next();
               if (inputCharacter.equalsIgnoreCase("N")){
                  out.println(" Thanks for taking your time!");}
                     while (inputCharacter.equalsIgnoreCase("Y") && (inputNumber != 17)) {  
                        System.out.println();
                        System.out.print(" What is 5 + 12?" + '\n' + " ");
                        inputNumber = keybardInput.nextDouble();
                           if (inputNumber == 17) {
                           out.println(" You are correct!");
                           }
                              else {
                                 System.out.println();
                                 System.out.println(" You are incorrect!");
                                 System.out.print(" Do you want to give it another try? Y/N"  + '\n' + " ");
                                 inputCharacter = keybardInput.next();
                                    if (inputCharacter.equalsIgnoreCase("N")){
                                       out.println(" Thanks for taking your time!");
                                    }
                              }
                     }
         }     
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.