Results 1 to 3 of 3
  1. #1
    ROD_PAX-A-LOT is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Trouble with running total

    Brand new to the forum, let me know if I am doing anything wrong. I am working on a currency calculator program. I would like it to display running totals of var1, var2,var3, qty1,qty2,qty3 and total when quiting (else if choice ==0) the program. This is as close as I could get it. I hope it is a simple logic/arithmetic error. The problem I have is that I cannot get it to store an accurate total when going to choice get new rates(else if choice ===9). Any help is greatly appreciated, I have been stuck on this for a couple of days now.






    Java Code:
    package foreigncurrency;
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class ForeignCurrency {
        static double rEUR=0, rGBP=0, rJPY=0, total=0, qty1=0, qty2=0, qty3=0, val1=0, val2=0, val3=0 ;
        static Scanner sc = new Scanner(System.in);
        
        public static void main(String[] args) {
           System.out.println("Welcome to the currency calculator.");
           getRates();
           doValuation();
           System.out.println("Thanks for using the currency calculator.");
        }//end of main
        
        public static void getRates() {
          System.out.println(
          "Please enter the currency rate Per US $ of the following currencies: "); 
          do {
             try {
             System.out.print("EUR: ");
             rEUR = sc.nextDouble();
             System.out.print("GBP: ");
             rGBP = sc.nextDouble();
             System.out.print("JPY: ");
             rJPY = sc.nextDouble();
             if (rJPY <=0 || rGBP <=0 || rEUR <=0) {
                System.out.println(
                        "Rates must be positive values. Please re-enter");
             }
          } catch (Exception e) {
              System.out.println("Please enter a valid currency rate");
              sc.next();
          }  
          }  while (rJPY <= 0 || rGBP <= 0 || rEUR <= 0);
        }
    
        public static void doValuation() {
            //System.out.println("EUR=" + rEUR + ", GBP=" + rGBP + ", JPY=" + rJPY);
            
            int choice=0;
            NumberFormat c = NumberFormat.getCurrencyInstance();
            do {
                System.out.print("Select Currency (1=EUR, 2=GBP, 3=JPY, 9=New Rates, 0=Quit): ");
                try {
                choice = sc.nextInt();
                
                if (choice == 1) {
                    System.out.print("How many Euros are you buying: ");
                    qty1 = sc.nextDouble();
                    if (qty1 <0) {
                        System.out.println("Quanity can't be negative");
                    } else {
                        val1 = qty1 * rEUR;
                        System.out.println(qty1 + " Euros = " + c.format(val1) );
                    } 
                } else   if (choice == 2) {
                   System.out.append("How many Pounds Sterling are you buying: ");
                   qty2 = sc.nextDouble();
                   if (qty2 <0) {
                       System.out.println("Quanity can't be negative");
                   } else {
                       val2 = qty2 * rGBP;
                       System.out.println(qty2 + " Pounds = " + c.format(val2) );
                   }
                } else if (choice == 3) {
                    System.out.print("How many yen are you buying: ");
                    qty3 = sc.nextDouble();
                    if (qty3 <0 ) {
                      System.out.println("Quantity can't be negative.");
                    } else{    
                        val3 = qty3 * rJPY;
                        System.out.println(qty3 + " YEN = " + c.format(val3) );
                   } 
                } else if (choice == 9) {
                      val1 = (val1 + val1);
                      val2 = (val2 + val2);
                      val3 = (val3 + val3);        
                      qty1 = (qty1 + qty1);
                      qty2 = (qty2 + qty2);
                      qty3 = (qty3 + qty3);
                      total = (val1 + val2 + val3 + total);
                      getRates();
                
                } else if (choice == 0) {
                    qty1 = (qty1 + qty1);
                    qty2 = (qty2 + qty2);
                    qty3 = (qty3 + qty3);
                    val1 = (val1 + val1);
                    val2 = (val2 + val2);
                    val3 = (val3 + val3);
                    System.out.println("EUR:" + qty1 + " units costing $" + val1);
                      System.out.println("GBP:" + qty2 + " units costing $" + val2);
                      System.out.println("JPY:" + qty3 + " units costing $" + val3);
                      total = (val1 + val2 + val3);
                      System.out.println("Thus the total value of the proposed currency purchases was: $" + (total));
                           
                } else if (choice != 0) {
                    System.out.println("I did not understand your choice.");
                   } 
    
            }  catch (Exception e) {
                System.out.println("Please enter a valid currency amount");
                sc.next();
            }
       
            }
          while (choice != 0); 
                
             
        }   
    }

  2. #2
    Ronin is online now Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Trouble with running total

    HI ROD_PAX-A-LOT, welcome.

    When you said you can't store an accurate result, what exactly is it doing in comparison with what you are expecting?

    Regards.

  3. #3
    ROD_PAX-A-LOT is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: Trouble with running total

    Thank you Ronin for replying. I think I finally have it solved. I had to add a couple of variables ex. v = (var1 +v) around lines 86 and 76 . I couldnt get the program to display totals for the quanity of currency, value(money spent per currency), or total of all currencies when quitting. I had it to where the total of all currencies was working properly, then when I tried to get the other totals to compute total would not put out the total value, it was not the sum of all the currencies(sometimes the sum of only the last two). I am going to do some more debugging to make sure that is is working properly and that my duplicates of variables for choice==9 and choice==0 is necessary. Thanks again

Similar Threads

  1. running total for loop structure.
    By downWithJava in forum New To Java
    Replies: 18
    Last Post: 03-04-2013, 10:22 PM
  2. Trying to create a running total.
    By skybeorn in forum New To Java
    Replies: 13
    Last Post: 03-16-2012, 07:27 PM
  3. Keeping track of running total help
    By masterasia in forum JCreator
    Replies: 1
    Last Post: 03-25-2011, 07:26 AM
  4. Running total little help
    By silverspoon34 in forum Forum Lobby
    Replies: 9
    Last Post: 02-27-2011, 09:19 PM
  5. Running Total by Key Group in HashMap
    By KevMull in forum New To Java
    Replies: 3
    Last Post: 11-16-2009, 03:44 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
  •