Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2004, 01:38 AM   PM User | #1
ragol_67
Regular Coder

 
Join Date: Sep 2002
Location: Calgary, AB
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
ragol_67 is an unknown quantity at this point
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.


Quote:
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);
}

}
ragol_67 is offline   Reply With Quote
Old 10-26-2004, 09:52 PM   PM User | #2
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Quote:
sub_total = sub_total + cheque;
actually be
Quote:
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..
Jason is offline   Reply With Quote
Old 10-27-2004, 07:57 AM   PM User | #3
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
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.
Roelf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:13 AM.


Advertisement
Log in to turn off these ads.