Solved

Random

Posted on 1998-07-22
3
165 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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