Hello,

I'm attempting a calculator program, but I keep getting cannot find symbol errors, I believe I called everything I needed and initialized the variables, I do not know what's wrong. Here is the code:

import java.util.Scanner;
import java.io.*;

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

        do
        {

            Scanner s = new Scanner(System.in);

            System.out.println("-Easy Calculator-");

            System.out.println("1) Add");

            System.out.println("2) Subtract");

            System.out.println("3) Multiply");

            System.out.println("4) Divide");

            System.out.println("5) Exit");

            System.out.println("Enter Option: ");

            int i = s.nextInt();


            System.out.println("First Number: ");
            int a = s.nextInt();
            System.out.println("Your first number number is " +a);


            System.out.println("Second Number: ");
            int b  =s.nextInt();
            System.out.println("Your second number number is " +b);


            double result = 0;




            switch (i)
            {
                case 1:
                result = a + b;
                break;


                case 2:
                result = a - b;
                break;

                case 3:
                result = a * b;
                break;


                case 4:
                if (b == 0)
                {
                    System.out.println("Cannot Divide By Zero");
                    break;
                }

                else
                result = a / b;

                default:
                System.out.println("You have entered a wrong choice");
            }

        } while(i != 5);


        System.out.println("Answer = " +result);
 }

}

Here are my errors

C:\Users\Desktop\calculator.java:8: error: class Calculator is public, should be declared in a file named Calculator.java
public class Calculator
       ^
C:\Users\Desktop\calculator.java:80: error: cannot find symbol
        } while(i != 5);
                ^
  symbol:   variable i
  location: class Calculator
C:\Users\Desktop\calculator.java:83: error: cannot find symbol
        System.out.println("Answer = " +result);
                                        ^
  symbol:   variable result
  location: class Calculator
3 errors

Recommended Answers

All 5 Replies

Actually I figured out some of it, I'm getting the errors because of my Do while loop, is there a way to implement that with my switch statements?

3 errors:
First is self-explanatory
while(i...
i is defined inside the while loop's {}, so it's not accessible outside those {}. Define it before starting the loop.
Ditto result - defined inside the loop so not accessible outside it. Define it before starting the loop

Yes, I just figured that out, but now when I compile it it will say the variables are not initialized

Means what it says... the compiler can see that it's possible to reach a statement where you use those variables before you have given them a value.
Easiest thing is to initialise them when you declare them, eg
int i = 0;

Now it compiles, but will not loop

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.