0

I recently made a program to be a calculator but three errors occurred. Here is the code:

import java.util.Scanner;
public class mathyStuff {
    public static void main(String[] args) throws InterruptedException {
        Scanner raw = new Scanner(System.in);
        String input = raw.nextLine();
        int y = 0;
        while (y < input.length()) {
            if (input.substring(y, y+1) == "+" || input.
            substring(y, y+1) == "-" ||  input.substring(y, y+1) == "/" ||  input.substring(y, y+1) == "*") {
                String x = input.substring(y, y+1);
                int z1 = Integer.parseInt(input.substring(0,y));
                int z2 = Integer.parseInt(input.substring(y+1, 0));
            }
            else {
                y = y + 1;
            }
        }
        math(z1,x,z2);
    }
    public static void math (int num1, String op, int num2) throws InterruptedException {
        if (op == "+") {
            System.out.println(String.valueOf(num1 + num2));
        }
        if (op == "-") {
            System.out.println(String.valueOf(num1 - num2));
        }
        if (op == "*") {
            System.out.println(String.valueOf(num1 * num2));
        }
        if (op == "/") {
            System.out.println(String.valueOf(num1 / num2));
        }
    }
}

Here is the errors:

Compilation Errors Detected

Line: 18
cannot find symbol
  symbol:   variable z1
  location: class mathyStuff

Line: 18
cannot find symbol
  symbol:   variable x
  location: class mathyStuff

Line: 18
cannot find symbol
  symbol:   variable z2
  location: class mathyStuff

I'm currently using a website called browxy, an online java compiler. And yes, I know. Download eclipse. I can't bring my computer everywhere I go so I use this instead.

Max
  • 1
  • 2
  • Beside problem with scope you should also correct way you [compare strings](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo May 06 '15 at 22:09
  • 2
    Yours is a scope problem. Your variables are only visible within the scope that they've been declared in. – Hovercraft Full Of Eels May 06 '15 at 22:09

2 Answers2

1

This is a scope issue.

You can't access x, z1 and z2 because they're declared inside a while loop, but you're trying to access them outside the while loop.

You probably want to move the math function call inside the if block inside the while loop.

Aify
  • 3,543
  • 3
  • 24
  • 43
  • I already edited my answer for that 10 minutes ago - move the math(z1, x, z2) call inside the if block that's inside the while loop. – Aify May 06 '15 at 22:21
0

You are essentially asking for a variable in a situation where java can't confirm that the variable has been defined. You need to pre-define the variable in a location outside any while loops or if loops that end before the calling of these variables.
Try adding this right before the int y = 0;:
int z1 = 0; int z2 = 0;
Also, where do you define x? If you mean the string x, you need to write "x" on line 18. If you mean a variable named x, make sure it is defined in the right scope. As it is, I can't find it anywhere.

Side note: your math method can't take "x" as an input for the second variable. It only takes "+", "-", "*", and "/". If you want to be able to use "x", you will need to change your math method.

Patrick vD
  • 684
  • 1
  • 7
  • 24