| | |
Switch Statement
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 23
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
switch (numberBoxes) { case 1: outPut = "Thank you - We hope to see you again!"; break; case 2: outPut = "Please visit our website at <a rel="nofollow" class="t" href="http://www.SuperXWarehouse.com" target="_blank">www.SuperXWarehouse.com</a>"; break; } 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.
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'.

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'.
•
•
Join Date: Feb 2007
Posts: 23
Reputation:
Solved Threads: 0
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
All of this code appears in the Message Dialog
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?
I have a variable called outPut
Java Syntax (Toggle Plain Text)
String outPut = "message from switch";
All of this code appears in the Message Dialog
Java Syntax (Toggle Plain Text)
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.
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.
well... I hope I understood you right this time, and this answer can help you on your way
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.
Java Syntax (Toggle Plain Text)
if (amountSpend < 1000){ choice = 1; } else{ if(amountSpend == 1000){ choice = 3; // the number in choice here doesn't matter, since it has to // be a default message } else{ if(amountSpend > 1000) choice = 2; } } // right now you have an integer you can use for your switch statement switch(choice){ case 1: outPut = "Message1"; break; case 2: outPut = "Message2"; break; default: outPut = "defaultMessage"; }
well... I hope I understood you right this time, and this answer can help you on your way
![]() |
Similar Threads
- switch statement on String in Java (Java)
- C# beginner switch statement (C#)
- switch statement (C)
- Switch Statement, Fall-Through. (C)
- Problems with switch statement (C++)
- Problems with switch statement (C++)
Other Threads in the Java Forum
- Previous Thread: help with java.awt textfields
- Next Thread: <b>TextFileStats.java need help!</b>
Views: 1865 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time tree web windows





