Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: random numbers in array please help

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default random numbers in array please help

    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...


     
    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);
    Last edited by copeg; November 14th, 2010 at 08:42 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: random numbers in array please help

    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
    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

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: random numbers in array please help

    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.*;

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: random numbers in array please help

    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.

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: random numbers in array please help

    o ok overlooked the equal sign sorry....
    and thank you.. and uhm 1 more thing, how can i align the arrays?
    is there a command like printf for arrays?

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: random numbers in array please help

    First off, you're not printing an array. Each time through the loop, you're printing an array element which happens to be an int.

    Also, there's no need to concatenate an empty String. println knows how to handle an int, as well as various other primitive types and Objects. Read the API:PrintStream (Java Platform SE 6)

    db

Similar Threads

  1. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  2. Java Question Need Help Restarting the Values of Variables from Random Numbers So Out
    By JavaStudent1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 02:25 PM
  3. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM
  4. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM