Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Wednesday, January 21, 2009

Random String Display From Array Program

import java.util.Vector;

/**

* Declare an array with 5 string and print them randomly.

*

*

*/

public class ShuffleArray {

// string array of five elements that will be printed randomely

private static String[] strArray = { "first", "second", "third", "fourth",

"fifth" };

// vector to store printed string position

private static Vector vecPrintLog = new Vector(5);

/**

* randomely print strings of array

*/

public static void printShuffleArray() {

System.out.println("Displaying String Randomely :: ");

// display random string

while (vecPrintLog.size() <>

// get random no between 1-10 then divide it by 2

int intNo = (int) ((Math.random() * 10) / 2);

// if same position again then just continue with new one

if (vecPrintLog.contains(Integer.toString(intNo)))

continue;

// print the string and log its position

System.out.println("\t" + strArray[intNo]);

vecPrintLog.add(Integer.toString(intNo));

}

}

/**

* exection begin from here

*

* @param args

* command line arguments

*/

public static void main(String[] args) {

printShuffleArray();

}

}

No comments:

Post a Comment

Followers