# Thread: Many Switches with Many Instances

1. Member
Join Date
Mar 2014
Posts
7
Rep Power
0

## Many Switches with Many Instances

Hello I am new to Java and I am trying to use values that are set for cases in the switch operator.
The first menu ask for you to pick a product and each product has a price on it.
The second menu ask you to pick a state with each state having a decimal value on it.
The third is asking you to put the number of cases and each case is 12 items.
A key note to remember is that each part that a person is choosing is on a different instance!

Here is an example of what i am trying to do.
Menu 1: I picked case 1 that is Computer and it is worth 1000
Menu 2: I picked case 1 that is CT and it's tax is 7.5
Third choice: I picked case 1 and that has 12 items

I want the subtotal witch is: (1000 * 12)
Subtotal in this situation is: 120000

Next i need the total value which is based on what state they picked for the tax percent value picked from the state menu case: (12000 * 0.075 + 120000)
Total value is: 129000

I will post the code I have but based on the choices a person makes will determine the values and I need those values set in the cases to put in a math equation. The problem I am having is retrieving these numbers form the cases inside the menu options and they are on a different instance. So How can I do this in Java code?

Here is the code:

Java Code:
```import java.util.*; //scanners and more
System.out.println ( "1) Computer \n2) Car \n3) TV \n4) Phone \n5) IPAD" ); //menu options
System.out.print ( "Selection: " );//Display for selection
}
Scanner in = new Scanner ( System.in );//scanner#1

switch ( in.nextInt() ) {
case 1:
int Computer = (1000);
System.out.println ( "You picked Computer, Price: 1000 per item." );//if 1 is picked
break;
case 2:
int Car = (13000);
System.out.println ( "You picked Car, Price: 13000 per item." );//if 2 is picked
break;
case 3:
int TV = (500);
System.out.println ( "You picked TV, Price: 500 per item." );//if 3 is picked
break;
case 4:
int Phone = (298);
System.out.println ( "You picked Phone, Price: 298 per item." );//if 4 is picked
break;
case 5:
System.out.println ( "You picked IPAD, Price: 699 per item." );//if 5 is picked
break;
default:
System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
new menu();//gives the user another choice to make
break;
}
}
public static void main ( String[] args) {
new cases();

}
}```

Java Code:
```import java.util.*; //scanners and more
System.out.println ( "1) CT 7.5% Tax \n2) VT 7.8% Tax \n3) WI 6.8% Tax \n4) CA 7.2% Tax \n5) WA 6.4% Tax" );//State menu options with taxes
System.out.print ( "Selection: " );//Display for selection
}
Scanner out = new Scanner ( System.in );//scanner #2

switch ( out.nextInt() ) {
case 1:
double CT = (7.5);
System.out.print ( "You picked CT 7.5% Tax" );//if 1 is picked
break;
case 2:
double VT = (7.8);
System.out.println ( "You picked VT 7.8% Tax" );//if 2 is picked
break;
case 3:
double WI = (6.8);
System.out.println ( "You picked WI 6.8% Tax" );//if 3 is picked
break;
case 4:
double CA = (7.2);
System.out.println ( "You picked CA 7.2% Tax" );//if 4 is picked
break;
case 5:
double WA = (7.4);
System.out.println ( "You picked WA 7.4% Tax" );//if 5 is picked
break;
default:
System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
new statemenu();//gives the user another choice to make
break;
}
}
public static void main ( String[] args) {
}
}```
Here is choice 3:

Java Code:
```import java.util.*;
class cases{
public void cases_amount () {
System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
System.out.print ( "Selection: ");
}
public cases() {
Scanner left = new Scanner ( System.in );

switch ( left.nextInt() ) {
case 1:
int choice1 = (12);
System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
break;
case 2:
int choice2 = (24);
System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
break;
case 3:
int choice3 = (36);
System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
break;
case 4:
int choice4 = (48);
System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
break;
case 5:
int choice5 = (60);
System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
break;
default:
System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
new cases();//gives the user another choice to make
break;
}
}
public static void main ( String[] args) {
new cases();
}
}```
Last edited by Combo; 03-24-2014 at 05:18 AM.

2. ## Re: Many Switches with Many Instances

[code]
[/code]
to get highlighting and preserve formatting.

problem I am having is retrieving these numbers
Are you talking about the contents of the variables that are locally defined in the switch statement?
Move their definitions outside the switch statement so that they are at the same scope as where you want to use them.
Last edited by Norm; 03-24-2014 at 12:23 AM.

3. Member
Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Many Switches with Many Instances

Yes the variables that are locally defined in the switch statement and I did edit to wrap my code with code tags and it looks much better.
When you say move their definition outside the switch statement do you mean change this code below:
From this?

Java Code:
```import java.util.*;
class cases{
public void cases_amount () {
System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
System.out.print ( "Selection: ");
}
public cases() {
Scanner left = new Scanner ( System.in );

switch ( left.nextInt() ) {
case 1:
int choice1 = (12);
System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
break;
case 2:
int choice2 = (24);
System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
break;
case 3:
int choice3 = (36);
System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
break;
case 4:
int choice4 = (48);
System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
break;
case 5:
int choice5 = (60);
System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
break;
default:
System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
new cases();//gives the user another choice to make
break;
}
}
public static void main ( String[] args) {
new cases();
}
}```
To this?

Java Code:
```import java.util.*;
class cases{
public void cases_amount () {
System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
System.out.print ( "Selection: ");
}
public cases() {
Scanner left = new Scanner ( System.in );
int choice1 = left.nextInt(1);
int choice2 = left.nextInt(2);
int choice3 = left.nextInt(3);
int choice4 = left.nextInt(4);
int choice5 = left.nextInt(5);
choice1 = 12;
choice2 = 24;
choice3 = 36;
choice4 = 48;
choice5 = 60;

switch ( left.nextInt() ) {
case 1:
System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
break;
case 2:
System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
break;
case 3:
System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
break;
case 4:
System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
break;
case 5:
System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
break;
default:
System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
new cases();//gives the user another choice to make
break;
}
}
public static void main ( String[] args) {
new cases();
}
}```

4. ## Re: Many Switches with Many Instances

Does the code work now?

5. Member
Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Many Switches with Many Instances

The code does compile and works, now I am trying to use those variables on a different instance.
I even added public int to my code but still having trouble using it on different instances.
I am thinking of using a IF and Else IF but having trouble making it work.
If you have a better suggestion let me know.
How would I use the variable if the person picks 1 in cases as their choice and picks 1 as their choice in the menu.
Here is cases code,
Java Code:
```import java.util.*;
class cases{
public int choice1;
public int choice2;
public int choice3;
public int choice4;
public int choice5;
public void cases_amount () {
System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
System.out.print ( "Selection: ");
}
public cases() {
Scanner left = new Scanner ( System.in );
int choice1 = left.nextInt(1);
int choice2 = left.nextInt(2);
int choice3 = left.nextInt(3);
int choice4 = left.nextInt(4);
int choice5 = left.nextInt(5);
choice1 = 12;
choice2 = 24;
choice3 = 36;
choice4 = 48;
choice5 = 60;

switch ( left.nextInt() ) {
case 1:
System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
break;
case 2:
System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
break;
case 3:
System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
break;
case 4:
System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
break;
case 5:
System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
break;
default:
System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
new cases();//gives the user another choice to make
break;
}
}
public static void main ( String[] args) {
new cases();
}
}```
Java Code:
```import java.util.*; //scanners and more
public int Computer;
public int Car;
public int TV;
public int Phone;
System.out.println ( "1) Computer \n2) Car \n3) TV \n4) Phone \n5) IPAD" ); //menu options
System.out.print ( "Selection: " );//Display for selection
}
Scanner in = new Scanner ( System.in );//scanner#1
int Computer = in.nextInt(1);
int Car = in.nextInt(2);
int TV = in.nextInt(3);
int Phone = in.nextInt(4);
Computer = 1000;
Car = 13000;
TV = 500;
Phone = 298;

switch ( in.nextInt() ) {
case 1:
System.out.println ( "You picked Computer, Price: 1000 per item." );//if 1 is picked
break;
case 2:
System.out.println ( "You picked Car, Price: 13000 per item." );//if 2 is picked
break;
case 3:
System.out.println ( "You picked TV, Price: 500 per item." );//if 3 is picked
break;
case 4:
System.out.println ( "You picked Phone, Price: 298 per item." );//if 4 is picked
break;
case 5:
System.out.println ( "You picked IPAD, Price: 699 per item." );//if 5 is picked
break;
default:
System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
new menu();//gives the user another choice to make
break;
}
}
public static void main ( String[] args) {
new cases();

}
}```

6. ## Re: Many Switches with Many Instances

The code has a problem defining variables with the same names at the class level and inside of a method. The variables in the method "shadow" the ones at the class level. It is NOT good to have local variables use the same names as variables defined at the class level.

Also The code gets values for the variables from the user and then immediately assigns them new values in lines 15 to 24 which replace and loses what the user entered.

When getting input from the user, it is better to ask ONE question and get ONE response from the user instead of like the code does (lines 15-19) where it gets 6 values from the user without any prompts to the user.

7. Member
Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Many Switches with Many Instances

Ok, is there a way to save what the user entered in the code then use that saved input to use it somewhere else?
Last edited by Combo; 03-24-2014 at 04:23 PM.

8. ## Re: Many Switches with Many Instances

Yes. The posted code has most of the statements to do that. but they are in the wrong places and there are several extra statements that are causing problems.

Sample flow:
0) define variable to hold user's response
2)read user's response and save it in variable
...
later in the code the value that was saved in 2) will be available
Last edited by Norm; 03-24-2014 at 04:25 PM.

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Many Switches with Many Instances

Very confusing; I get the idea that the code is supposed to ask the user what the numbers are for computer, tv, etc. , but that is at this point only half implemented.

Java Code:
```public menu() {
Scanner in = new Scanner ( System.in );
int Computer = in.nextInt(1);                      // ???
int Car = in.nextInt(2);
int TV = in.nextInt(3);
int Phone = in.nextInt(4);
Why are those five reads from the keyboard there?

10. Member
Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Many Switches with Many Instances

Is there anyway you can point me in the right direction and where I should put them. I know you already said to
not have local variables use the same names as variables defined at the class level.
and I understand that part but I don't know where to put the variables.

11. Member
Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Many Switches with Many Instances

Why are those five reads from the keyboard there?
Because I have no idea what I am doing with Java code. I was told to put the variables outside of the switch operator so I though it would be put there. Right now I understand that is the wrong place. If you can please tell me where I would put this at it would be great.

12. ## Re: Many Switches with Many Instances

Defining a variable
and assigning a value to a variable
can be done at different places in the code.

If several methods need to access a variable, then that variable should be defined at the class level, like in lines 3 to 7.
Values can be assigned to those variables or read from those variables in any method in the class.

Before reading a user's input, the user needs to be asked a question so he knows what data to enter.

One problem you are having is trying to do 6 things at once.
It is better to do one thing at a time.

Suggestion: Simplify the code. Work on getting choice1 ONLY, remove the other 5 choice variables. Write code to get the value for choice1 and use it. When that works, then move to working with choice2. etc
Last edited by Norm; 03-24-2014 at 05:13 PM.

13. Member
Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Many Switches with Many Instances

Ok, thanks I will work on this

14. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Many Switches with Many Instances

Originally Posted by Combo
Because I have no idea what I am doing with Java code. I was told to put the variables outside of the switch operator so I though it would be put there. Right now I understand that is the wrong place. If you can please tell me where I would put this at it would be great.
... No, I really cannot. Sorry that I asked a question assuming you understood your own code, pretend I didn't say anything.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•