Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-28-2008, 02:32 PM
Member
 
Join Date: Apr 2008
Posts: 2
vexity is on a distinguished road
showing results in a for loop randomly
Hello there, I would like to display results from a from loop randomly and I have no idea how to do it. This is the for loop that i have:

Code:
for (int i = 0; i < object.length(); i++) { System.out.println(displaytext(i)); }
I basically want to display some text I have stored in variables else where, but i don't want to display them serially, i want to show them randomly. I also don't want any of the text to be displayed more then once.
All help is appreciated, thanks!

Last edited by vexity : 04-28-2008 at 02:33 PM. Reason: forgot to mention something
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-28-2008, 02:35 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Where your texts are stored. In an array, if so find the index randomly and get the text.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-28-2008, 06:17 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Because of your "display once" requirement you need to implement a more complicated solution. You have to implement a shuffle algorithm. For this you can randomly shuffle an array of indexes to your objects and then display the text at each shuffled index. To shuffle the array of indexes you can go once through the array with a for loop and for each index swap the value with another randomly picked location in the array. Sorry I don't have time to write the code right now. Maybe later if you still need it.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-29-2008, 03:32 AM
Member
 
Join Date: Apr 2008
Posts: 2
vexity is on a distinguished road
I'd appreciate it much if you can still show me the code.
-thanks
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-29-2008, 06:24 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, try this code.

Code:
import java.util.Arrays; import java.util.Random; /** * * @author Eranga Tennakoon */ public class vexity { private String[] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"}; private Random random = new Random(); public static void main(String[] args) { new vexity().doSelection(); } private void doSelection() { int[] index = new int[numbers.length]; Arrays.fill(index, -1); for (int i = 0; i < index.length; i++) { int num = getNumber(index); index[i] = num; System.out.println("random value: " + num); System.out.println("word : " + numbers[num]); } } private int getNumber(int[] array) { int n; do { n = random.nextInt(numbers.length); } while(isContain(array, n)); return n; } private boolean isContain(int[] array, int n) { for(int i = 0; i < array.length; i++) { if(array[i] == n) return true; } return false; } }
What you have tried up to now pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with displaying search results from an array BHCluster New To Java 4 04-24-2008 05:34 AM
Returning multiple results (JDBC 3.0) JavaForums Java Blogs 0 04-18-2008 05:20 PM
date and calender not getting the right results valoyivd New To Java 4 04-14-2008 01:51 PM
BigInteger remainder results in zero perito New To Java 1 03-21-2008 06:07 PM
results to code disappear too fast for DOS window dubdubdub New To Java 3 12-29-2007 07:07 PM


All times are GMT +3. The time now is 04:07 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org