Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: problems with loop in Java App

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy problems with loop in Java App

    Hello I am trying to create a Java Application where the user enters the quantity of items from a catalogue and the price of each item. The application should display the total cost of the required items. The user can purchase more than one item at a time.

    If they receive a 11% discount if they buy 5 or more of an item.

    The problem is the the loop only allows you enter in the quantity of items and when you quit it will not display the total for the customer.

    I have only started learning Java a few months ago. I used a tutorial where you have one input dialog box as a basis for the code below, but I am not sure how I can get the loop to work and display the results with two input dialog boxes. tried everything I know. If anyone I can help I would appreciate. Thanks.


     
    package loop;
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
     
    public class Loop {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
      int itemCounter,
        itemValue,
        itemQuantity,
        total,
        discount;
     
         itemCounter=0;
        itemValue =0 ;
        itemQuantity=0;
        total=0;
     
    String valueStr,
            shoppingStr,
           quantityStr;
     
    while (itemCounter !=-1){
     
     
    total= itemQuantity * itemValue;
     
    itemCounter+=itemCounter;
     
     quantityStr = JOptionPane.showInputDialog("enter item quantity or -1 to quit");
     
     itemQuantity = Integer.parseInt(quantityStr);
     
     
    valueStr = JOptionPane.showInputDialog("enter item quantity or -1 to quit");
     
    itemValue = Integer.parseInt(valueStr);
     
    }
    DecimalFormat twoDigits = new DecimalFormat ("0:00");
    if (itemCounter >=5) {
     
    discount =  (int) (total * 0.11);
    }
    else {
    discount = 0;
    }
    JOptionPane.showMessageDialog (null, "the total cost is "+twoDigits.format(discount));
          }
        }
    Last edited by helloworld922; March 13th, 2010 at 07:58 PM.


Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 03:09 AM
  2. [SOLVED] Problems setting up java files that I downloaded from a publisher's website
    By vendetta in forum Java Theory & Questions
    Replies: 1
    Last Post: February 10th, 2010, 03:43 PM
  3. for loop in java
    By kissmiss in forum Loops & Control Statements
    Replies: 1
    Last Post: December 16th, 2009, 03:47 AM
  4. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 08:43 PM
  5. Replies: 1
    Last Post: April 1st, 2009, 03:47 PM