DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005

    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.
    } // End of main() method.

    } // End of MonthlyBills class.

    Can anyone help complete this??


  2. #2
    Join Date
    Aug 2005
    the loop would probably start something like:
    Please enter billType:
    while(billType != "done"){
                    Please enter billType:

  3. #3
    Join Date
    Jul 2005
    the Netherlands
    Quote Originally Posted by Code_Nerd
    the loop would probably start something like:
    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:

    System.out.println("Please enter billType: ");
    billType = /* read input from user */;
        stringAmount = /* read input from user */;
        billAmount = Double.parseDouble(stringAmount);
        sum = sum + billAmount;
        System.out.println("Please enter billType: ");
        billType = /* read input from user */;

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2005, 02:00 PM
  2. How to loop through rows in Datalist control?
    By Acceris in forum ASP.NET
    Replies: 0
    Last Post: 06-30-2005, 02:31 PM
  3. Problem with a loop
    By salvinger in forum VB Classic
    Replies: 0
    Last Post: 05-07-2005, 01:38 PM
  4. sql query - totals by week
    By israfel in forum Database
    Replies: 2
    Last Post: 03-10-2005, 03:33 PM
  5. Replies: 0
    Last Post: 06-12-2000, 03: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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.