• November 14th, 2010, 07:17 PM
gonfreecks
Code java:

```public class Arrays { public static void main(String[] args) { Scanner input = new Scanner(System.in);   int x, y =0; int [] list; list = new int [10]; int [] list2; list2 = new int [10]; x =(int)(99*Math.random()+1); y=(int)(99*Math.random()+1); int array = 9; int [] list3; list3 = new int [10]; int [] list4; list4 = new int [10]; int i;     System.out.print("List 1 : "); for (i = 0 ; i<=list.length ; i++) list [i] = x =(int)(99*Math.random()+1); System.out.print(list[i]);```
i dont really know much yet because its out first time dealing with arrays
i want to input a random value between 1-99 in list[0]-list[9] so i used a for loop is this possible?

i previously have this other code which is really long so i wondered if i could use a for loop please help...

Code java:

```  list[0] = x; x =(int)(99*Math.random()+1); list[1] = x; x =(int)(99*Math.random()+1); list[2] = x; x =(int)(99*Math.random()+1); list[3] = x; x =(int)(99*Math.random()+1); list [4] = x; x =(int)(99*Math.random()+1); list [5] = x; x =(int)(99*Math.random()+1); list [6] = x; x =(int)(99*Math.random()+1); list [7] = x; x =(int)(99*Math.random()+1); list [8] = x; x =(int)(99*Math.random()+1); list [9] = x; x =(int)(99*Math.random()+1);   list2[0] = x; x =(int)(99*Math.random()+1); list2[1] = x; x =(int)(99*Math.random()+1); list2[2] = x; x =(int)(99*Math.random()+1); list2[3] = x; x =(int)(99*Math.random()+1); list2[4] = x; x =(int)(99*Math.random()+1); list2[5] = x; x =(int)(99*Math.random()+1); list2[6] = x; x =(int)(99*Math.random()+1); list2[7] = x; x =(int)(99*Math.random()+1); list2[8] = x; x =(int)(99*Math.random()+1); list2[9] = x; x =(int)(99*Math.random()+1);```
• November 14th, 2010, 08:46 PM
copeg
For future reference, please surround your code with the [highlight=java][/highlight]

Yes a loop is possible. I see you have a loop in the first snippet, a few changes
Code :

```for (i = 0 ; i<=list.length ; i++){ list [i] =(int)(99*Math.random()+1); System.out.print(list[i]); }```

First note the curly brackets, otherwise the println doesn't know what i is. Second, why assign x for every loop? See The for Statement for more info
• November 14th, 2010, 09:04 PM
gonfreecks
thanks so much its working now but i dont get why an error keeps popping..

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at lab8.Arrays.main(Arrays.java:35)

dont get it... so i deleted x and went with your code....

so i have this one

System.out.print("List 1 : ");
for (i = 0 ; i<=list.length ; i++){
list [i] =(int)(99*Math.random()+1);
System.out.print(list[i]+ " ");
}

so after i run that i output

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at lab8.Arrays.main(Arrays.java:35)
List 1 : 31 59 36 63 35 6 53 23 88 40 Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

this so in every box of the array it outputs a random number from 1-99.. so far it works but yeah i dont know whats causing the error..

do i import something?

so far i inputted

import java.util.Scanner;
import java.io.*;
• November 14th, 2010, 09:17 PM
copeg
Think about the loop and how arrays are zero based: with an array of size 10, array[10] is out of bounds for the array - an exception will be thrown. See the red in the following:

for (i = 0 ; i<=list.length ; i++){

Once again, please surround your code with the code tags I mentioned above.
• November 14th, 2010, 10:30 PM
gonfreecks