Switch Statement

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster

Switch Statement

 
0
  #1
May 3rd, 2007
I have a question I am writing this program for class where all of the output goes into one JOptionPane.showMessageDialog for instance I have the The sub total price displayes, the sales tax displayed, and the Grand total displayed in this I also want to display a message if the persons grand total is < $1000.00 "Thank you for your business" else if it is > $1000.00 " Thank you if you need any help please let us know"

I was trying to do the case statement where it is has case 1: case 2: Can I do an if statement in the switch to where all of this messages will be in the same Message Dialog? I hope this makes sense. thank you in advance for any help

  1. switch (numberBoxes)
  2. {
  3. case 1:
  4. outPut = "Thank you - We hope to see you again!";
  5. break;
  6.  
  7. case 2:
  8. outPut = "Please visit our website at <a rel="nofollow" class="t" href="http://www.SuperXWarehouse.com" target="_blank">www.SuperXWarehouse.com</a>";
  9. break;
  10. }
  11.  
  12. JOptionPane.showMessageDialog(null, "Your order will be shipped in " + numberBoxes + " boxes." + "\n\n\nThe price for " + numberDevices + " Devices is: $ " + twoD.format(ttlPrice) +"\nSales Tax @ 7.95%: $" + twoD.format(salesTax) + "\nTotal Price: $" + twoD.format(grandTotal) + "\n" + outPut);
Last edited by countrygirl1970; May 3rd, 2007 at 3:34 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Switch Statement

 
0
  #2
May 3rd, 2007
if I understand you correctly, which I hope I do, otherwise my answer won't mean that much to you
you want a number of answers, determined by the case clauses, all in the same MessageDialog, like
"
Answer one
Answer two
...
Last answer
"

if this is the problem, the sollution is quite easy.
just declare a String 'output' before you start the switch statement and then just add every answer to this same String object.
at the end of the statements, when all the answers you want are stored in the String object, display the String in the JOptionPane.MessageDialog

for instance:

String output = "";
int answer = readAnswer();

while(busy){

switch(answer){
case 1: output += "\n" + wantedAnswer;
// wantedAnswer is a String containing the message you would
// would like to show for this case
break;
case 2: output += "\n" + wantedAnswer2;
}
int answer = readAnswer();


if(answer == 0)
busy = false;
}

basicly, you're just adding content to one and the same String object, and afterwards, you can use this String object (output) as text to display in your messageDialog.
the \n will automatically force the text to start at a new line, so the output will be formatted and will look a bit 'organized'.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster

Re: Switch Statement

 
0
  #3
May 3rd, 2007
Thank you for your help I really appreciate it. I am going to go try and do what you said to do but I am not sure I understand exactly what you are saying. Maybe I did not make it clear what I was trying to do here goes again. LOL be patient with me I am new to Java but I am learning.

I have a variable called outPut
  1. String outPut = "message from switch";

All of this code appears in the Message Dialog
  1. JOptionPane.showMessageDialog(null, "Your order will be shipped in " + numberBoxes + " boxes." + "\n\n\nThe price for " + numberDevices + " Devices is: $ " + twoD.format(ttlPrice) +"\nSales Tax @ 7.95%: $" + twoD.format(salesTax) + "\nTotal Price: $" + twoD.format(grandTotal) + "\n" + outPut);

But on the outPut part I want to make it choose either case 1 or
case 2 depending on if the total price is < $1000 or > $1000. I don't know if this is making much since. Ok if the customer buys less than $1000 then case 1 message should be displayed but if the customer buys more than $ 1000 then case 2 message should be displayed if the customer bought exactly $1000 then a default message will be displayed. Does that make since?
Last edited by countrygirl1970; May 3rd, 2007 at 6:16 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Switch Statement

 
0
  #4
May 3rd, 2007
so it's just one answer you want to give, and the if you asked about was just wether you could put the if in the switch statement?
well... you can allways 'nest' statements, but in this case I would have to say no.

since you have 3 possible positions, put the if-statement before the switch-statement, and make (or decide the value of) a 'new' int choice.
  1. if (amountSpend < 1000){
  2. choice = 1;
  3. }
  4. else{
  5. if(amountSpend == 1000){
  6. choice = 3;
  7. // the number in choice here doesn't matter, since it has to
  8. // be a default message
  9. }
  10. else{
  11. if(amountSpend > 1000)
  12. choice = 2;
  13. }
  14. }
  15.  
  16.  
  17. // right now you have an integer you can use for your switch statement
  18. switch(choice){
  19. case 1: outPut = "Message1";
  20. break;
  21. case 2: outPut = "Message2";
  22. break;
  23. default: outPut = "defaultMessage";
  24. }

well... I hope I understood you right this time, and this answer can help you on your way
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1865 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC