Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > array to generate words

Reply
Thread Tools

array to generate words

 
 
IanH
Guest
Posts: n/a
 
      03-02-2006
I need to generate a message box with each of these random words but
cant figure out what im doing wrong, can anyone help?


import javax.swing.*;
public class random
{
public static void main (String args[])
{
String text[];
text = new String[5];
string increment;
int number = 0;

text[number++] = "Monday";
text[number++] = "Tuesday";
text[number++] = "Wednesday";
text[number++] = "Thursday";
text[number++] = "Friday";

increment = Math.floor(Math.random() * number);

JOptionPane.showMessageDialog(null, increment);

}

}

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      03-02-2006

"IanH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I need to generate a message box with each of these random words but
> cant figure out what im doing wrong, can anyone help?
>
>
> import javax.swing.*;
> public class random
> {
> public static void main (String args[])
> {
> String text[];
> text = new String[5];
> string increment;
> int number = 0;
>
> text[number++] = "Monday";
> text[number++] = "Tuesday";
> text[number++] = "Wednesday";
> text[number++] = "Thursday";
> text[number++] = "Friday";
>
> increment = Math.floor(Math.random() * number);
>
> JOptionPane.showMessageDialog(null, increment);
>
> }
>
> }


Did you try actually running your program? What output does it produce?
Is this the desired output? If not, where would the source of the erroneous
output be coming from? For example, fi the program is printing out "Canada",
"America", "Germany", etc. instead of days of the week, I'd be looking at
the parts of my code which contain names of countries, and ask myself how
did that get into the data flow leading to the message dialog.

What is it outputting, and where might that data becoming from?

- Oliver

 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      03-02-2006

"IanH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I need to generate a message box with each of these random words but
> cant figure out what im doing wrong, can anyone help?
>
>
> import javax.swing.*;
> public class random
> {
> public static void main (String args[])
> {
> String text[];
> text = new String[5];
> string increment;
> int number = 0;
>
> text[number++] = "Monday";
> text[number++] = "Tuesday";
> text[number++] = "Wednesday";
> text[number++] = "Thursday";
> text[number++] = "Friday";
>
> increment = Math.floor(Math.random() * number);
>
> JOptionPane.showMessageDialog(null, increment);
>
> }
>
> }
>

If I understand you correctly, you are populating the 'text' array with five
Strings and want to choose one of them at random and display that String. If
that is right, the following untested code should do the job:

/* Create random number generator. */
Random random = new Random();
/* Choose an integer between 0 and the size of the array; effectively
chooses the index of one element of the array. */
int randomIndex = Math.abs(random.nextInt()%text.length);
/* Display the randomly-chosen day. */
JOptionPane.showMessageDialog(null, "The random day is " + text[randomInt]);

--
Rhino



 
Reply With Quote
 
IanH
Guest
Posts: n/a
 
      03-02-2006
Hi Oliver,

I know its not outputting correctly, the output is numbers instead of
the days of the week. Is this to do with number++, not sure new to java
so appreciate suggestions.

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-02-2006
"IanH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi Oliver,
>
> I know its not outputting correctly, the output is numbers instead of
> the days of the week. Is this to do with number++, not sure new to java
> so appreciate suggestions.
>


Well, you can try an experiment. Remove all the number++, replacing them
with something else (perhaps 0 or something), and see if that affects the
output.

Another technique would be to pretend to be the computer, and read the
source code and try to decide what you would do at each step if you were the
computer, to try to spot the problem.

- Oliver

 
Reply With Quote
 
James Westby
Guest
Posts: n/a
 
      03-02-2006
IanH wrote:
> Hi Oliver,
>
> I know its not outputting correctly, the output is numbers instead of
> the days of the week. Is this to do with number++, not sure new to java
> so appreciate suggestions.
>

try

JOptionPane.showMessageDialog(null, text[increment]);


James

 
Reply With Quote
 
Rhino
Guest
Posts: n/a
 
      03-02-2006

"Rhino" <(E-Mail Removed)> wrote in message
news:71HNf.4706$(E-Mail Removed).. .
>
> "IanH" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>I need to generate a message box with each of these random words but
>> cant figure out what im doing wrong, can anyone help?
>>
>>
>> import javax.swing.*;
>> public class random
>> {
>> public static void main (String args[])
>> {
>> String text[];
>> text = new String[5];
>> string increment;
>> int number = 0;
>>
>> text[number++] = "Monday";
>> text[number++] = "Tuesday";
>> text[number++] = "Wednesday";
>> text[number++] = "Thursday";
>> text[number++] = "Friday";
>>
>> increment = Math.floor(Math.random() * number);
>>
>> JOptionPane.showMessageDialog(null, increment);
>>
>> }
>>
>> }
>>

> If I understand you correctly, you are populating the 'text' array with
> five Strings and want to choose one of them at random and display that
> String. If that is right, the following untested code should do the job:
>
> /* Create random number generator. */
> Random random = new Random();
> /* Choose an integer between 0 and the size of the array; effectively
> chooses the index of one element of the array. */
> int randomIndex = Math.abs(random.nextInt()%text.length);
> /* Display the randomly-chosen day. */
> JOptionPane.showMessageDialog(null, "The random day is " +
> text[randomInt]);
>

Oops, that last line should be:

JOptionPane.showMessageDialog(null, "The random day is " +
text[randomIndex]);

--
Rhino


 
Reply With Quote
 
IanH
Guest
Posts: n/a
 
      03-02-2006

James Westby wrote:

> IanH wrote:
> > Hi Oliver,
> >
> > I know its not outputting correctly, the output is numbers instead of
> > the days of the week. Is this to do with number++, not sure new to java
> > so appreciate suggestions.
> >

> try
>
> JOptionPane.showMessageDialog(null, text[increment]);
>
>
> James


 
Reply With Quote
 
IanH
Guest
Posts: n/a
 
      03-02-2006
Thanks Rhino

I have a problem with the Random random = new random, am i supposed to
have a class created for this?

 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      03-02-2006
On Thu, 02 Mar 2006 18:43:16 -0000, Rhino
<(E-Mail Removed)> wrote:
> If I understand you correctly, you are populating the 'text' array with
> five
> Strings and want to choose one of them at random and display that
> String. If
> that is right, the following untested code should do the job:
>
> /* Create random number generator. */
> Random random = new Random();
> /* Choose an integer between 0 and the size of the array; effectively
> chooses the index of one element of the array. */
> int randomIndex = Math.abs(random.nextInt()%text.length);
> /* Display the randomly-chosen day. */
> JOptionPane.showMessageDialog(null, "The random day is "
> + text[randomInt]);


The randomIndex would be better to use the nextInt method that takes a
parameter:

http://java.sun.com/j2se/1.5.0/docs/...ml#nextInt(int)

The main reason in this case is because it's simpler, but you should also
be aware that performing modulo arithmetic on random numbers introduces
bias.

Dan.


--
Daniel Dyer
http://www.dandyer.co.uk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
split camelcase string into array of words words pantagruel Javascript 8 07-22-2006 07:46 PM
Words Words utab C++ 6 02-16-2006 07:00 PM
Non-noise words are incorrectly recognised as noise words. Peter Strĝiman ASP .Net 1 08-23-2005 01:26 PM
Re: A little bit of help regarding my linked list program required. - "words.c" - "words.c" Richard Heathfield C Programming 7 10-05-2003 02:38 PM



Advertisments