im working on this java program:
Create a Java program that creates six random numbers between 1 and 10 and for each randomly generated number it displays the text describing the number rather than the numerical value. For instance, if the number generated was 3 then it should display three; if the number generated was 6 then it should display six.

so far my code generates the random number but i dont know how to dosplay the number in text...please help

my code so far:

import java.util.*;

public class RadomNumbers {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub


            Random rand = new Random();
            int num = rand.nextInt(10);
            System.out.println("Generated Random Number between 0 to 10 is : " + num);

          }
        } 

Since the range of the random numbers are between 1 and 10, you can create a String array with those numbers:

String [] numbers = {"one", "two", ...};

Then after you get the random number use it as index at the array. You must not forget that the index of the arrays start from 0.

Then after you get the random number use it as index at the array. You must not forget that the index of the arrays start from 0.

thanxs but coud you please explain this bit a bit more???

String [] array = {"one", "two", ...} ;
System.out.println(array[[B]0[/B]]);
System.out.println(array[[B]1[/B]]);

latinajoyce,
just a small addition.
In your problem description you tell us you need to choose numbers between 1 and 10. However your code chooses numbers between 0 (included) and 10 (excluded).
I don't know what you really need but, if you need numbers between 1 and 10 (both included) you'd better use int num = rand.nextInt(10) +1;

import java.util.*;
public class RadomNumbers {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String [] numbers = {" ", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
Random rand = new Random();
int num = rand.nextInt(10)+1;
System.out.println("Generated Random Number between 0 to 10 is : " + numbers[num]);

}
}

Daiva,
did you read what javaAddict mentioned about arrays being zero based?

import java.util.*;
public class RadomNumbers {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String [] numbers = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
Random rand = new Random();
int num = rand.nextInt(10)+1;
System.out.println("Generated Random Number between 0 to 10 is : " + numbers[num]);

}
}

Good for you that you gave him the complete solution, when freelancelote and I were trying to help the poster learn. As if we couldn't do what you did. Not to mention that your code is WRONG

Can you tell me freelancelote, please, did I correct my mistake in the code above? (I edited my message)

Sorry for my bad code and for posting it, I also try to learn...

Daiva,
it does the trick.
I wouldn't have add an unnecessary member to the array, though. Instead I would have called the correct one when you need to call it. But this is just my opinion.

Oh, I understand now.
Thank you.
And sorry again for my interrupt with a wrong code. I have just started learning JAVA.

From the API this will return numbers from 0 to 9: rand.nextInt(10) So this:

int num = rand.nextInt(10)+1;
System.out.println("Generated Random Number between 0 to 10 is : " + numbers[num]);

will give you numbers from 1 to 10. The array has indexes from 0 to 9. So you will get an exception.

Also from what I understood we don't want numbers from 1 to 10, but to display randomly one of these: "one", "two", "three", ...
So it wouldn't matter even if the numbers generated were from 53 to 62 as long they are mapped to one of the String we want to return.

Naturally numbers from 0 to 9 were chosen because they are easily mapped to the values of an array with size 10 ([0],...,[9]).

Also the values of the array could be mixed:

String [] numbers = { "five", "one", "three", "four", "two", "six", "seven", "ten", "eight", "nine"};

We don't need them sorted. All we need is a random number between 0 to 9. As long as it is random, we will get randomly one of the elements of the array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.