Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 09-30-2007, 10:45 AM   #1
Beta Member
Join Date: Sep 2007
Posts: 1
Default Java Help

This is a very simple problem but im having serious problems!!!

Im making a simple program for uni in a basic java class and im needing to select a random string from an array.

this is my method code that i need help with..

public static void correct()
String[] reply = {"Well Done", "Excellent!", "Your Hot!", "Very good!", "Your On Fire!"};

JOptionPane.showMessageDialog (null, reply);


this gives me the whole array as the reply in the diologue box, i need it to give a random string as the reply.

Any help will be greatly appriecated!!

petrides is offline   Reply With Quote
Old 09-30-2007, 07:07 PM   #2
Baseband Member
Join Date: Aug 2007
Posts: 96
Default Re: Java Help

I haven't done Java in literally years... So, I'll give you a very general suggestion. It's up to you to make it work.

Use Math.Random to get a random integer between 0-4, then use the JOptionPane..... to show reply[randomInt].

Premium Web Hosting Provider ║ MambugHost.com
Need to upload files? ║ MambugLoad.com
Fast, Effective, Web Design ║ Mambug.com
Mambug is offline   Reply With Quote
Old 10-01-2007, 08:20 PM   #3
Daemon Poster
xtremezx2's Avatar
Join Date: Sep 2006
Posts: 915
Default Re: Java Help

As stated above, you need to give the index of which string you want. just putting reply in with most languages would give an error, but seems that java just gives you everything, interesting. What mambug stated would be a great way to do it and should be very simple to implement (1 or 2 lines max).
xtremezx2 is offline   Reply With Quote
Old 10-11-2007, 10:08 AM   #4
In Runtime
CGfreak102's Avatar
Join Date: Apr 2007
Posts: 475
Default Re: Java Help

it would look somthing like this,

public static void correct()
int randarray = 0;
String[] reply = {"Well Done", "Excellent!", "Your Hot!", "Very good!", "Your On Fire!"};
Math.random() * 6;

JOptionPane.showMessageDialog (null, reply[randarray]);


or at least something like that,
Core I7 920 OC'ed to 3.0Ghz, 6GB G.skill DDR3 1333, Intel DX58SO, 500GB seagate 7200RPM + 320GB seagate, EVGA 9800GTX+ , 600W PSU.
CGfreak102 is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 04:10 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0