Solved

Random

Posted on 1998-07-22
3
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 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