Jump to content

Java help !

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Hey C.C, I have a minor small problem.

I have right now headache and can't think of a way to randomize a String array.
I just can't get it to work. I have like rewritten it over 9000(seriously I have done it like that). So my last savior is you guys. Care to help me out?

Thanks in advance :)

Edit: My array is build like this in a object... String[] s = new String[3];
s[0]="Test";
s[1]="This";
s[2]="Thing";
So anyway to randomize their position later?

Edited by Turk4n, 08 December 2008 - 06:37 AM.
Forgot to tell you guys...

Posted Image

#2
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts
By looking at this: Koders Code Search: Randomizer.java - Java I think your solution is in sight. If not, I will open up a development environment and test it for you myself :)

Phil

#3
Exe

Exe

    Newbie

  • Members
  • Pip
  • 7 posts
You are adding those strings to indexes of an array of strings. What are indexes represented by and what will nextInt() for the Random give you? Put two and two together.

#4
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

morefood2001 said:

By looking at this: Koders Code Search: Randomizer.java - Java I think your solution is in sight. If not, I will open up a development environment and test it for you myself :)

Phil

It works as a charm, I even shorten it since most things were not needed for my little thing :), thank you.
Posted Image

#5
Maze

Maze

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
So could you put your little thing code here; I really want to know how you randomize it.

#6
Maze

Maze

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I try and do it;

The code is here :

// Demonstrate String arrays.
import java.util.*;
class StringDemo {
 
  public static void main(String args[]) { 
	  
    String str[] = { "one", "two", "three" };
    
    for(int i=0; i<str.length; i++) {
    	
    	int n = (int)(3.0 * Math.random());

      System.out.println("str[" + i + "]: " +
                          str[(int) n]);
  }
}
}


#7
-xAsN

-xAsN

    Newbie

  • Members
  • Pip
  • 7 posts
in other words, use Math.random function to randomly select a number within the string, indicated by the [], thereafter displaying it via println. hope it helps!:)

#8
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Still, I got it fixed by using the advice Philip gave out, thanks again Phil!
Posted Image

#9
Maze

Maze

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts

-xAsN said:

in other words, use Math.random function to randomly select a number within the string, indicated by the [], thereafter displaying it via println. hope it helps!:)

The same as i write :)

#10
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Maze said:

The same as i write :)

Indeed, however I have gotten my help so thanks lol :)
Posted Image