Results 1 to 3 of 3

Thread: Another Newbie Java Problem

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Resolved 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.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width