Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    Calgary, AB
    Posts
    179
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why don't loops work properly inside methods? (JAVA)

    Here is the code I am working with.

    The first problem I am having is that when I run it, the loop in the method "che_amo" doesn't work properly. It is supposed to keep asking for a cheque number and the cheque amount, until the user enters a -1 for a cheque number, then it is supposed to move on. But what happens is once the user enters in a -1 for a cheque number, it asks them again for a cheque number, and they must enter another -1 in order for it to move on.

    If I take out "sub_total = che_amo();" in the method "gst", then the problem is solved. What is the reason for this?


    My second problem is very similar to the first, except that the line "gst_amo = sub_total * gst;" in the method "gst" is returning the value of sub_total * gst, but it isn't taking the actual sub total, it is just taking the value that I declared the subtotal to be in the method "che_amo".

    Could someone please help me figure this out? I would be greatly appreciative.

    Thank you,
    Nick Young.


    public class SimpleMethods
    {
    /**
    * replace this comment with your own
    *
    */
    public static void main()
    {

    balance();
    header();
    che_amo();
    gst();


    }


    //End of Main
    //////////////////////////////////////////////////////////
    //Start of Methods

    public static double balance()
    {
    double balance; //The starting balance of users account.
    final double ovr_dft = 50.00; //Overdraft on users account. In this program, it is always 50 dollars!

    balance = IO.readDouble("Please Enter Your Current Account Balance: ");
    System.out.println("Your Current Account Balance is: " + balance);
    System.out.println("You have an overdraft protection of " + ovr_dft);
    System.out.println();

    return balance;
    }


    public static void header()
    {
    System.out.print("Cheque No.");
    System.out.print(" ");
    System.out.print("Amount");
    System.out.println();
    }


    public static double che_amo()
    {
    double cheque,
    amount,
    sub_total = 0.0;

    cheque = IO.readDouble("Please Enter Cheque Number: ");
    while (cheque > 0)
    {
    System.out.print(" " + cheque);
    amount = IO.readDouble("Please Enter Cheque Amount: ");
    System.out.print(" " + amount);

    sub_total = sub_total + cheque;
    cheque = IO.readDouble("Please Enter Cheque Number: ");
    System.out.println();
    }

    return sub_total;
    }


    public static void gst()
    {
    double sub_total,
    gst = 0.07,
    gst_amo,
    total;

    sub_total = che_amo();
    gst_amo = sub_total * gst;
    System.out.println("The total GST is: " + gst_amo);
    total = sub_total + gst;
    System.out.println("You Owe a Total of: " + total);
    }

    }

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    for your first problem I would probably have to suggest that you use a do-while statement instead of a while statement. That should cut that -1 x 2 problem down.

    for your second problem, I would suggest that you make a global value for subtotal instead of instances of the same value in different methods. Though I don't see why that wouldn't work as you have it, I haven't stared at it enough yet perhaps...


    Jason

    edit:
    shouldn't the line
    sub_total = sub_total + cheque;
    actually be
    sub_total = sub_total + amount;
    to give you an actual total? why would you add check numbers?

    are you sure the subtotal at the end of che_amo() is the number you want? did you check with a print statement to make sure?

    Jason
    Last edited by Jason; 10-26-2004 at 09:58 PM.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    for the first problem, you give your own solution. After calling the method che_amo() from the main(), you call it again from the gst() method. So the user has to end the loop twice. changing the while loop to a do..while loop doesnt help this. A do..while loop executes always once, a while loop doesn't have to execute at all, make sure you get the desired functionality in sight before you choose a program structure inhere.

    Overall i have to mention the following: why don't you use the return value from your functions. You probably just need the returnvalue from your che_amo() function in the gst() function. So remove the call to che_amo() from the main() method and you should be fine. Don't you have to use the return from the balance method anywhere?

    Seems to me this (probably homework assignment) still needs a lot of work.
    I am the luckiest man in the world


  •  

    Posting Permissions

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