switch statement problem

I can't call the variables in the case statements

Page 1 of 1

4 Replies - 1067 Views - Last Post: 24 October 2007 - 06:59 AM Rate Topic: -----

#1 clovers25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-October 07

switch statement problem

Posted 23 October 2007 - 05:07 AM

I'm new here and I hope you can help me with this program...

This program is similar to a "vending" machine. The user will choose among the variety of clothes' brands and sizes. At the end of the program, the machine will display the grand total...

The problem is I can't get the grand total. It always say "cannot find symbol - variable stt1,st2......

import java.io.*;
import javax.swing.*;
public class Case4 {
public static void main (String[]args) throws IOException {
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));




System.out.println("First Order");
System.out.println("[1]Giordano");
System.out.println("[2]Bench");
System.out.println("[3]Tag");
System.out.println("[4]Polo");

System.out.println("Enter Brand Code: ");

String xx = br.readLine();
int x = Integer.parseInt(xx);


switch(x)
{
case 1:
System.out.println("Giordano:");
System.out.println("Sizes");
System.out.println("[1]Small");
System.out.println("[2]Medium");
System.out.println("[3]Large");
break;

case 2:
System.out.println("Bench:");
System.out.println("Sizes");
System.out.println("[4]Small");
System.out.println("[5]Medium");
System.out.println("[6]Large");
break;

case 3:
System.out.println("Tag:");
System.out.println("Sizes");
System.out.println("[7]Small");
System.out.println("[8]Medium");
System.out.println("[9]Large");
break;

default:
System.out.println("Polo:");
System.out.println("Sizes");
System.out.println("[10]Small");
System.out.println("[11]Medium");
System.out.println("[12]Large");
break;
}



System.out.println("Enter Size");
String yy = br.readLine();
int y = Integer.parseInt(yy);


switch(y)
{

case 1: System.out.println("Giordano Small");
System.out.println("P200.00");
System.out.println("Enter Quantity");
String qq1 = br.readLine();
int q1 = Integer.parseInt(qq1);
int st1 = (q1*200);
System.out.println("Subtotal: "+st1);
break;


case 2: System.out.println("Giordano Medium");
System.out.println("P250.00");
System.out.println("Enter Quantity");
String qq2 = br.readLine();
int q2 = Integer.parseInt(qq2);
int st2=(q2*200);
System.out.println("Subtotal: "+st2);
break;


case 3: System.out.println("Giordano Large");
System.out.println("P300.00");
System.out.println("Enter Quantity");
String qq3 = br.readLine();
int q3 = Integer.parseInt(qq3);
int st3=(q3*300);
System.out.println("Subtotal: "+st3);
break;


case 4: System.out.println("Bench Small");
System.out.println("P300.00");
System.out.println("Enter Quantity");
String qq4 = br.readLine();
int q4 = Integer.parseInt(qq4);
int st4=(q4*300);
System.out.println("Subtotal: "+st4);
break;


case 5: System.out.println("Bench Medium");
System.out.println("P350.00");
System.out.println("Enter Quantity");
String qq5 = br.readLine();
int q5 = Integer.parseInt(qq5);
int st5=(q5*350);
System.out.println("Subtotal: "+st5);
break;



case 6: System.out.println("Bench Large");
System.out.println("P400.00");
System.out.println("Enter Quantity");
String qq6 = br.readLine();
int q6 = Integer.parseInt(qq6);
int st6=(q6*400);
System.out.println("Subtotal: "+st6);
break;


case 7: System.out.println("Tag Small");
System.out.println("P300.00");
System.out.println("Enter Quantity");
String qq7 = br.readLine();
int q7 = Integer.parseInt(qq7);
int st7=(q7*300);
System.out.println("Subtotal: "+st7);
break;



case 8: System.out.println("Tag Medium");
System.out.println("P400.00");
System.out.println("Enter Quantity");
String qq8 = br.readLine();
int q8 = Integer.parseInt(qq8);
int st8=(q8*400);
System.out.println("Subtotal: "+st8);
break;



case 9: System.out.println("Tag Large");
System.out.println("P500.00");
System.out.println("Enter Quantity");
String qq9 = br.readLine();
int q9 = Integer.parseInt(qq9);
int st9=(q9*500);
System.out.println("Subtotal: "+st9);
break;



case 10: System.out.println("Polo Small");
System.out.println("P250.00");
System.out.println("Enter Quantity");
String qq10 = br.readLine();
int q10 = Integer.parseInt(qq10);
int st10=(q10*300);
System.out.println("Subtotal: "+st10);
break;



case 11: System.out.println("Polo Medium");
System.out.println("P300.00");
System.out.println("Enter Quantity");
String qq11 = br.readLine();
int q11 = Integer.parseInt(qq11);
int st11=(q11*300);
System.out.println("Subtotal: "+st11);
break;



default: System.out.println("Polo Large");
System.out.println("P400.00");
System.out.println("Enter Quantity");
String qq12 = br.readLine();
int q12 = Integer.parseInt(qq12);
int st12=(q12*300);
System.out.println("Subtotal: "+st12);
break;
}


/*problem is here*/
double gt=(st1+st2+st3+st4+st5+st6+st7=St8+st9+st10+st11+st12);
System.out.println("Grandtotal: "+gt);
/*problem is here*/





}

}




This post has been edited by clovers25: 23 October 2007 - 05:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: switch statement problem

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: switch statement problem

Posted 23 October 2007 - 05:56 AM

you are defining the variables inside the switch statement, thus when they reach the } they are out of scope and lost.
Try doing it like this:
int st1 = 0;

switch(y)
{

case 1: System.out.println("Giordano Small");
System.out.println("P200.00");
System.out.println("Enter Quantity");
String qq1 = br.readLine();
int q1 = Integer.parseInt(qq1);
st1 = (q1*200);
System.out.println("Subtotal: "+st1);
break;


as an example of the first case. The st# variable is defined outside, and changed inside, thus it's value will be accessible at the end, after your switch statement is done. This will need to be done for all the variables you wish to read like this.
Was This Post Helpful? 0
  • +
  • -

#3 clovers25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-October 07

Re: switch statement problem

Posted 24 October 2007 - 12:48 AM

I tried your code but another error appears here

(double gt=(st1+st2+st3+st4+st5+st6+st7=St8+st9+st10+st11+st12)

it says "unexpected type" =(

any more suggestions sir?
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: switch statement problem

Posted 24 October 2007 - 12:57 AM

The problem lies with the sub-expression st7=St8 right around the middle of that line - you've got an the assignment operator in there, when you probably meant to use another + operator, and you've capitalized the S in st8. Missed that shift key by half a beat, it looks like :)

HTH,

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 clovers25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-October 07

Re: switch statement problem

Posted 24 October 2007 - 06:59 AM

Err thanks to both of you. Yep the errors are the wrong way of declaration and the typographical error....

I really appreciate your help. This is a great experience for a new member like me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1