Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Why don't loops work properly inside methods? (JAVA)
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

 Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
10-23-2004, 02: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
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); } }

10-26-2004, 10:52 PM   PM User | #2
Jason
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
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 10:58 PM..

 10-27-2004, 08:57 AM PM User | #3 Roelf 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

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:03 PM.

 AdvertisementLog in to turn off these ads.