Results 1 to 3 of 3
  1. #1
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default having trouple with arrays

    I am supposed to have this program working, but my arrays are empty, the values i give them don't seem to show, and i am getting 0's back for all i values. what am i doing wrong

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.Random;
    
    public class Array
    {
    	public static void main (String[] args)
    	{
    	
    	int [] randNum= new int[10]; //sets the max storage to 10 numbers
    	int [] triple= new int[10]; //sets the max storage to 10 numbers
    	int [] sum= new int [10];	//sets the max storage to 10 numbers
    	
    	String header=String.format("%s  %8s %8s %4s","","randomNums","tripleNums","sums");
    	System.out.println(header);
    	
    		randomNums(randNum); //send the array to method
    		triple(randNum,triple);//send the arrays to method
    		sum(randNum,triple,sum);//send the arrays to method
    				
    		for (int i=0;i<10;i++) //displays the arrays
    		{
    			String output=String.format("%d \t %d \t %d \t %d \t",i,randNum[i],triple[i],sum[i]);
    			System.out.println(randNum[i]); 
    		}
    	}
    	public static void randomNums(int [] randNum)//10 random ints 1-5
    	{
    		for (int i=0;i<10;i++)
    		{
    			int number; //creates an integer for the test
    			Random rand=new Random(); //declares a rand value
    			number=(rand.nextInt(5)+1);
    			System.out.println(number);// tests the random fuction to make sure it is working
    			randNum[i]=number; //assigns the random number to the array list
    		
    		}	
    	}
    	public static void triple(int triple[],
    					 int [] randNum)//multiplies the stored character in
    							//randNums by 3 and stores it in triple array
    	{	
    		for (int i=0;i<10;i++)
    		{
    			triple[i]= randNum[i]*3;
    		} 
    		
    	}
    	public static void sum( int [] randNum,
    									int [] triple,
    									int [] sum)//gets the sum of the arrays and stores them
    	{	
    		for (int i=0;i<10;i++)
    		{						
    			sum[i]= randNum[i]+triple[i];
    		
    		} 
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: having trouple with arrays

    Check the order of you parameters (e.g. you're passing randNum and triple (in this order) to your triple( ... ) method, but it expects the randNum array as a second parameter).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: having trouple with arrays

    thanks worked perfectly, it's funny how a little mistake in one method null my results.

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  2. 2D Arrays
    By NixasMuraki in forum New To Java
    Replies: 5
    Last Post: 03-17-2011, 08:53 AM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •