package test;
import java.util.Scanner;
public class Test {
    public static void main(String[] args){
        Scanner kbReader = new Scanner(System.in);
        System.out.print("Choose mode (\"words\" or \"symbols\"): ");
        String mode = kbReader.nextLine();
        if ("words".equals(mode))
        {
            String oa = " plus ";
            String os = " minus ";
            String om = " times ";
            String od = " divided by ";
            String e = " equals ";
        }
        else
        {
            String oa = " + ";
            String os = " - ";
            String om = " * ";
            String od = " / ";
            String e = " = ";
        }
        System.out.println("Choose your operation:");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Your choice: ");
        Scanner kbReader2 = new Scanner(System.in);
        int choice = kbReader2.nextInt();
        if (choice > 4)
        {    
            System.out.println("You must enter a 1, 2, 3, or 4.");
            return;
        }
        System.out.print("Enter first integer: ");
        int op1 = kbReader.nextInt();
        System.out.print("Enter second integer: ");
        int op2 = kbReader.nextInt();
        System.out.println("");
        switch (choice)
        {
            case 1:
                System.out.println(op1 + oa + op2 + e + (op1 + op2));
                break;
            case 2:
                System.out.println(op1 + os + op2 + e + (op1 - op2));
                break;
            case 3:
                System.out.println(op1 + om + op2 + e + (op1 * op2));
                break;
            case 4:
                System.out.println(op1 + od + op2 + e + ((double)op1 / op2));
                break;
        }
    }
}

This won't run correctly for some reason. It tells me that the variables oa, os, om, od, and e can not be found, but I declared and initialized them earlier in my first if-then statements...

The bottom part of your code cannot see those variables because they are enclosed in a different scope (inside different braces).

commented: Very helpful! +0

The bottom part of your code cannot see those variables because they are enclosed in a different scope (inside different braces).

I'm really new to Java, and not sure how to change that while preserving the function of my code. Any suggestions?

package operation_of_choice;
import java.util.Scanner;
public class Operation_of_choice {
    public static void main(String[] args) {
        Scanner kbReader = new Scanner(System.in);
        System.out.print("Choose mode (\"words\" or \"symbols\"): ");
        String mode = kbReader.nextLine();
        String oa;
        String os;
        String om;
        String od;
        String e;
        if ("words".equals(mode))
        {
            oa = " plus ";
            os = " minus ";
            om = " times ";
            od = " divided by ";
            e = " equals ";
        }
        else
        {
            oa = " + ";
            os = " - ";
            om = " * ";
            od = " / ";
            e = " = ";
        }
        System.out.println("Choose your operation:");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Your choice: ");
        Scanner kbReader2 = new Scanner(System.in);
        int choice = kbReader2.nextInt();
        if (choice > 4)
        {    
            System.out.println("You must enter a 1, 2, 3, or 4.");
            return;
        }
        System.out.print("Enter first integer: ");
        int op1 = kbReader.nextInt();
        System.out.print("Enter second integer: ");
        int op2 = kbReader.nextInt();
        System.out.println("");
        switch (choice)
        {
            case 1:
                System.out.println(op1 + oa + op2 + e + (op1 + op2));
                break;
            case 2:
                System.out.println(op1 + os + op2 + e + (op1 - op2));
                break;
            case 3:
                System.out.println(op1 + om + op2 + e + (op1 * op2));
                break;
            case 4:
                System.out.println(op1 + od + op2 + e + ((double)op1 / op2));
                break;
        }
    }
}

This fixed it! Thanks for telling me what was wrong, Thines01!

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.