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

switch (numberBoxes)
{
case 1:
outPut = "Thank you - We hope to see you again!";
break;
 
case 2:
outPut = "Please visit our website at www.SuperXWarehouse.com";
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);

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

Edited 3 Years Ago by happygeek: fixed formatting

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

String outPut = "message from switch";

All of this code appears in the Message Dialog

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?

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.

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 :)

This question has already been answered. Start a new discussion instead.