|
-
Nov 12th, 2005, 02:32 AM
#1
Thread Starter
Hyperactive Member
Another Newbie Java Problem
Okay take a look at this program that I was instructed to write from the book I have for Java:
Code:
import javax.swing.*; //import all the swing methods
public class ModifiedInvoiceApp //declare the InvoiceApp class
{
public static void main(String[] args) //main routine of InvoiceApp class
{
String choice = ""; //empty string
while (!(choice.equalsIgnoreCase("n"))) //begin while loop (while value of choice is not 'n' ignoring case)
{
String inputString = JOptionPane.showInputDialog("Enter order total: "); //get the order from the user
double orderTotal = Double.parseDouble(inputString); //reparse the input number so that it can be used
double discountAmount = 0; //empty double
if (orderTotal >= 500)
discountAmount = orderTotal * .20;
else if (orderTotal >= 250 && orderTotal < 500)
discountAmount = orderTotal * .15;
else if (orderTotal >= 100 && orderTotal < 250)
discountAmount = orderTotal * .10;
else if (orderTotal < 100)
discountAmount = 0;
double invoiceTotal = orderTotal - discountAmount; //set value of invoiceTotal
String message = "Order total: " + orderTotal + "\n"
+ "Discount amount: " + discountAmount + "\n"
+ "Invoice total: " + invoiceTotal + "\n\n"
+ "Continue (Y/N)? "; //message string for final dialog
choice = JOptionPane.showInputDialog(null, message, "Invoice Application", JOptionPane.INFORMATION_MESSAGE); //final dialog
}
System.exit(0); //close the thread from the JOptionPane methods
}
}
Okay here is the problem:
The line that looks like this:
Code:
while (!(choice.equalsIgnoreCase("n")))
It's checking for the wrong letter to continue. It should be "y". Well I changed it, compiled the program, and for some reason, it creates an infinite loop. However, when I change it back to "n", it doesn't create and infinite loop and runs the program like it should. It seems to be just a simple change of a letter and it creates an infinite loop. What is going on here? I can't figure it out. Thanks again.
Last edited by GamerMax5; Nov 17th, 2005 at 03:04 AM.
-
Nov 12th, 2005, 06:21 AM
#2
Re: Another Newbie Java Problem
what you have at the moment means
while the user has entered a value that is not n then loop again
while (!(choice.equalsIgnoreCase("n"))){}
replacing the n with y would mean
while the user has entered a value that is not y then loop again
while (!(choice.equalsIgnoreCase("n"))){}
to use y to continue the loop. replace n with y and remove the not
while(choice.equalsIgnoreCase("y")){}
-
Nov 12th, 2005, 01:09 PM
#3
Thread Starter
Hyperactive Member
Re: Another Newbie Java Problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|