Java Array Help.

Discussion in 'Mac Programming' started by Maleficum, Dec 3, 2006.

  1. Maleficum macrumors member

    Joined:
    Jun 29, 2006
    Location:
    In a Chair
    #1
    Code:
    class ArrayTotal
    {
    	private double[] numbers;
    
    	public ArrayTotal(double[] nums)
    	{
    		numbers = new double[nums.length];
    		for(int i=0; i<numbers.length;i++)
    		{
    			numbers[i]=nums[i];
    		}
    	}
    
    
          [b] public void setArray(double[] nums)
    	{
                   
                 setArray(numbers);
    
    
    	}
         [/b]
    		
    
    	public double getTotal()
    	{
    		double total=0;
                 for(int i=0; i<numbers.length; i++) 
                  { 
                    total=numbers[i]+numbers[i+1];
                    System.out.println(total);
                    }
                 
    		return total;
    	}
    
    	public String toString( )
    	{
    		String output="";
    		for (int h = 0; h<numbers.length; h++)
    		{
    			output+="number "+h + " :: " +String.format("%.2f",numbers[h])+"\n";
    		}
    		return "\n"+output;
    	}
    }
    
    public class Lab14a
    {
    	public static void main( String args[] )
    	{
    		ArrayTotal test = new ArrayTotal(new double[]{93.4,100.0,90.0});
    		System.out.println(test);
    	   System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
    
    		test.setArray(new double[]{50.0,100.0,80.0});
    		System.out.println(test);
    	   System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
    
    		test.setArray(new double[]{93.4,-90.0,90.0});
    		System.out.println(test);
    	   System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
    
    		test.setArray(new double[]{1,2,3,4,5,6,7,8,9});
    		System.out.println(test);
    	   System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
    	}
    }

    The point of this lab is to pull the numbers from the double[] arrays at the bottom of the code and total them up. I need help with what exactly to put in the bolded area (setArray). I tried what I have, but it pulled an infinite loop. I know my code in getTotal() is in correct, but I'll be fixing that later.
     
  2. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    Code:
    
    public void setArray(double[] nums)
    {  
      for(int i=0; i<nums.length; i++) 
      { 
         numbers[i] = nums[i];
      }
    }
    
    should fill the array.

    Using setArray within itself is an unrestricted recursive loop. I'm sure you'll learn about using recursion correctly sometime in the future. It's extremely powerful, even when done wrong. ;)
     
  3. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    You can count up the values in an array using the following
    Code:
    public static double arrayCounter(double[] theArray){[INDENT]double tot=0.0;
    for(int i=0;i<theArray.length;i++){
    [INDENT]tot=tot+theArray[i][/INDENT]
    }
    theArray=null;//to save on system resources.
    return tot
    [/INDENT]}
    This should work perfectly, you can feed it all your arrays and add up those totals if needed.
     
  4. bozigle macrumors regular

    #4
    Ot

    Hey
    you're +1 out of your array in getTotal()
     
  5. AndyS macrumors newbie

    Joined:
    Nov 13, 2006
    #5
    Indeed!
    Code:
     public double getTotal()
     {
      double total=0;
      for(int i=0; i<numbers.length; i++) 
      { 
              total=total+numbers[i]; //Otherwise you will get an IndexOutOfBounds 
              System.out.println(total);
      }
                 
    		return total;
    	}
    
     
  6. MarkCollette macrumors 68000

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Calgary, Canada
    #6
    I assume you were going more for something like:
    Code:
    public class ArrayTotal
    {
    	private double[] numbers;
    
    	public ArrayTotal(double[] nums)
    	{
    		setArray(nums);
    	}
    
    	public void setArray(double[] nums)
    	{
    		numbers = new double[nums.length];
    		for(int i=0; i<numbers.length;i++)
    		{
    			numbers[i]=nums[i];
    		}
    	}
         
    	public double getTotal()
    	{
    		double total=0.0;
    		for(int i=0; i<numbers.length; i++) 
    		{ 
    			total += numbers[i];
    		}
    		System.out.println(total);
    		return total;
    	}
    
    	public String toString( )
    	{
    		StringBuffer output = new StringBuffer();
    		output.append('\n');
    		for (int h = 0; h<numbers.length; h++)
    		{
    			output.append("number ");
    			output.append( Integer.toString(h) );
    			output.append(" :: ");
    			output.append( String.format("%.2f",numbers[h]) );
    			output.append('\n');
    		}
    		return output.toString();
    	}
    }
     

Share This Page