Results 1 to 3 of 3
Thread: Array method problem
- 12-04-2010, 04:24 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
Array method problem
Hi guys, I'm new to methods and having a problem passing the results of an array to a display method, the simple program is to enter four saving account balances and return the array + the fixed rate bonus.
I can use display methods with single variables, it's displaying the array that is throwing me!
here's the code:
Many thanks!
Java Code:import java.util.Scanner; class SaverMethods { public static void inputNums(double[] bal) { Scanner input = new Scanner(System.in); for(int x = 0; x < 4; x++) { System.out.println("Input four savers balances"); bal[x] = input.nextDouble(); } } public static double[] calcBonus(double[] bal) { Scanner input = new Scanner(System.in); double bonus; System.out.println("Please enter the percentage bonus"); bonus = input.nextDouble(); double total = 0, temp = 0; for(int x = 0; x < 4; x++) { temp = (bal[x] * bonus) / 100; total = bal[x] + temp; } return bal; } public static void displayTotal(double[] sortedList); { for(int x = 0; x < 4; x++) System.out.println(sortedList[x]); } public static void main(String[] args) { double[] balance = new double[4]; inputNums(balance); balance = calcBonus(balance); displayTotal(balance); } }
- 12-04-2010, 04:38 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
well you have 1 logic and 1 syntax error in your program:
1st one:
you need to get rid of the semicolon at the end of this method declaration.Java Code:public static void displayTotal(double[] sortedList);
Secondly, look at this loop of yours below:
at what point are you updating the bal[x] indexes to store the updated balances? you should have the third statment in body of loop something likeJava Code:for(int x = 0; x < 4; x++) { temp = (bal[x] * bonus) / 100; total = bal[x] + temp; }
Java Code:bal[x] = temp + total;
- 12-04-2010, 04:58 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
need help with question(method & array)
By highschool in forum New To JavaReplies: 5Last Post: 02-10-2010, 05:06 PM -
Passing an array to a method.
By twcast in forum New To JavaReplies: 9Last Post: 02-10-2010, 09:13 AM -
Problem using the toUpperCase method with character array...
By lisalala in forum New To JavaReplies: 2Last Post: 02-24-2009, 04:32 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks