Solved

Random

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 52
Crystal Reports Licensing Questions 4 56
Github api to fetch the number of collaborators in a git repo 1 25
Html split(text) 2 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

685 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