accumulating totals in a loop


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: accumulating totals in a loop

  1. #1
    Join Date
    Dec 2005
    Posts
    1

    Question accumulating totals in a loop

    I have a prewritten java program to calculate the total of monthly bills. It should execute until the user enters "done" instead of bill type. After the user enters a bill type, he should be asked to enter the bill amount. I don't have a clue where to start. The prewritten program is as follows....

    // MonthlyBills.java - This program calculates the total of your monthly bills.
    // Input: Bill type and bill amount.
    // Output: Prints the total of your monthly bills.

    import javax.swing.JOptionPane;

    public class MonthlyBills
    {
    public static void main(String args[])
    {

    String billType; // Description of bill.
    String stringAmount; // String version of bill amount.
    double billAmount; // Amount of the bill.
    double sum = 0; // Accumulates sum of bills.

    /* You should set up your loop to execute as long as the user
    has not entered the word done. You can use these input and
    output statements anywhere in your program. They are not in
    any particular order.
    */

    // This input statement asks the user to enter a bill type or the word none.
    billType = JOptionPane.showInputDialog("Enter bill type or the word done to quit.");

    // This input statement asks your user to enter a bill amount.
    stringAmount = JOptionPane.showInputDialog("Enter amount of bill");

    // This statement converts the string version of the amount to a double.
    billAmount = Double.parseDouble(stringAmount);

    // This statement displays the sum of monthly bills.
    System.out.println("Sum of monthly bills is $: " + sum);

    // This statement causes the program to exit.
    System.exit(0);
    } // End of main() method.

    } // End of MonthlyBills class.

    Can anyone help complete this??

    Thanks

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    the loop would probably start something like:
    Code:
    Please enter billType:
    while(billType != "done"){
                    .....
                    ....
                    ....
                    Please enter billType:
    }

  3. #3
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by Code_Nerd
    the loop would probably start something like:
    Code:
    Please enter billType:
    while(billType != "done"){
                    .....
                    ....
                    ....
                    Please enter billType:
    }
    No. With Strings (or any Objects) you're comparing references with the == or != operators, not their contents. Compare Strings like this:

    Code:
    System.out.println("Please enter billType: ");
    billType = /* read input from user */;
    
    while(!billType.equalsIgnoreCase("done")){
    
        stringAmount = /* read input from user */;
        billAmount = Double.parseDouble(stringAmount);
        sum = sum + billAmount;
    
        System.out.println("Please enter billType: ");
        billType = /* read input from user */;
    }
    [/QUOTE]

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2005, 03:00 PM
  2. How to loop through rows in Datalist control?
    By Acceris in forum ASP.NET
    Replies: 0
    Last Post: 06-30-2005, 03:31 PM
  3. Problem with a loop
    By salvinger in forum VB Classic
    Replies: 0
    Last Post: 05-07-2005, 02:38 PM
  4. sql query - totals by week
    By israfel in forum Database
    Replies: 2
    Last Post: 03-10-2005, 04:33 PM
  5. Replies: 0
    Last Post: 06-12-2000, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center