Results 1 to 3 of 3
  1. #1
    insanepenguin is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default 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);
    	}
    }

  2. #2
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    well you have 1 logic and 1 syntax error in your program:

    1st one:
    Java Code:
    public static void displayTotal(double[] sortedList);
    you need to get rid of the semicolon at the end of this method declaration.
    Secondly, look at this loop of yours below:
    Java Code:
    		for(int x = 0; x < 4; x++)
    		{
    		temp = (bal[x] * bonus) / 100;
    		total = bal[x] + temp;
    		
    		}
    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 like
    Java Code:
     bal[x] = temp + total;

  3. #3
    insanepenguin is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks a lot f1gh can't believe I missed that semi colon!

    You were right about the loop too, needed to add bal[x] = total to output the new balance on each iteration.

    At least I almost correct in returning the array to the main method!

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. need help with question(method & array)
    By highschool in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 06:06 PM
  3. Passing an array to a method.
    By twcast in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 10:13 AM
  4. Replies: 2
    Last Post: 02-24-2009, 05:32 PM
  5. Replies: 29
    Last Post: 09-25-2008, 08:55 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
  •