Solved

Random

Posted on 1998-07-22
3
151 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
Comment Utility
can you send your html page

0
 

Accepted Solution

by:
tomd012698 earned 100 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 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 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:

763 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

10 Experts available now in Live!

Get 1:1 Help Now