Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post I have a little problem with a code.

    The code i made was a random number generator that made numbers up to 20, and I took that random number and used it for an array table to put out a random statement. It works perfectly, but the problem I'm having is that the array i made can only put out numbers, and I want it to put out words instead. So overall my question is. how can I make an array say words instead of numbers? here's the code:


    import java.util.Random;

    public class test3
    {
    public static void main(String[] args) {
    Random number = new Random();
    int Number;

    for(int counter=1; counter<=1;counter++){
    Number = number.nextInt(20);

    int array[]=new int[20];

    array[0]=0;
    array[1]=1;
    array[2]=2;
    array[3]=3;
    array[4]=4;
    array[5]=5;
    array[6]=6;
    array[7]=7;
    array[8]=8;
    array[9]=9;
    array[10]=10;
    array[11]=11;
    array[12]=12;
    array[13]=13;
    array[14]=14;
    array[15]=15;
    array[16]=16;
    array[17]=17;
    array[18]=18;
    array[19]=19;

    System.out.println(array[Number]);
    }

    }

    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    But the answer is simple - replace the numbers which are the array elements by string values (in quotes).

    e.g.

    array[0]= "Something";

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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