Solved

Random

Posted on 1998-07-22
3
156 Views
Last Modified: 2010-03-30
I'm trying to make an applet which will randomly choose a string from an array loaded from <PARAM> tags.  I have little programming experience and have no idea how to do this.  Can you tell me how to get started?
0
Comment
Question by:6100
  • 2
3 Comments
 
LVL 4

Expert Comment

by:evijay
ID: 1227694
can you send your html page

0
 

Accepted Solution

by:
tomd012698 earned 100 total points
ID: 1227695
Here's your HTML applet tag...

<applet
    code=MyApplet.class
    name=MyApplet
    width=320
    height=440 >
    <param name=Strings value = "Smelly,Stinky,Spiteful">
</applet>

Your applet class could look something like this.

class MyApplet extends Applet
{
  // A Vector to store the strings.
  private Vector _strings = new Vector();

  public start()
  {
    // Get the parameter.
    // Use a StringTokenizer to extract all the tokens.
    // Place into the array

    String list = getParameter( "Strings" );
    StringTokenizer t = new StringTokenizer( list, ',' );

    while ( t.hasMoreTokens )
       _strings.addElement( t.nextToken() );
  }

  public String getRandomString()
  {
    int n = (int)(Math.random() * (_strings.size() - 1));
    return (String)_strings.elementAt( n );
  }
}  

This is a pretty simplistic case, but I hope it helps.  The strategy would remain basically the same.  Good luck.


0
 

Expert Comment

by:tomd012698
ID: 1227696
Just as a follow up...

The line:
  StringTokenizer t = new StringTokenizer( list, ',' );
should read:
  StringTokenizer t = new StringTokenizer( list, "," );

You should also be careful about randomly generating the array index to pull out the String.  The code I gave you doesn't check to make sure the array isnt empty.  You will access the array out of bounds if it is empty.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now