Results 1 to 3 of 3
Thread: having trouple with arrays
- 02-01-2012, 07:26 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
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]; } } }
- 02-01-2012, 08:05 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2012, 12:59 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
Similar Threads
-
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
2D Arrays
By NixasMuraki in forum New To JavaReplies: 5Last Post: 03-17-2011, 08:53 AM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks