We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,752 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

What's wrong with this?

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...

2
Contributors
3
Replies
45 Minutes
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
VIEBlitz
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

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?

VIEBlitz
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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!

VIEBlitz
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0634 seconds using 2.71MB