-1

Code that prints the contents of array randomly, the result recuired is print contents randomly, could do that with Math.Random();

public class Random {
    public static void main(String args[]) throws Exception {             
        String[] Answers = new String[4];
        Answers[0] = "OS";
        Answers[1] = "GOOD";
        Answers[2] = "CS";
        Answers[3] = "Cody";  

        for(int n=0;n<Answers.length;n++) {
            System.out.print(Answers);  
        }
    }
}      

the above code prints

[Ljava.lang.String;@1db9742[Ljava.lang.String;@1db9742[Ljava.lang.String;@1db9742[Ljava.lang.String;@1db9742

would want it to print CS,OS,GOOD, CODY or OS,CODY,GOOD,CS and so on

deHaar
  • 17,687
  • 10
  • 38
  • 51
jvm_code
  • 33
  • 1
  • 1
  • 8

8 Answers8

2

Simple solution

public static void permuteString(String beginningString, String endingString) {

        String[] answers = new String[4];
        answers[0] = "OS";
        answers[1] = "GOOD";
        answers[2] = "CS";
        answers[3] = "Cody";

        List<String> answersList = Arrays.asList(answers);
        Collections.shuffle(answersList);
        System.out.println(Arrays.toString(answersList.toArray(new String[0])));

    }
Michal
  • 970
  • 7
  • 11
1

You are printing the array's address or reference. You need to print elements stored in array so you need to access it with index. Since you want to add a Random functionality to it. You do Something like this-

import java.util.*;
//Random is itself a class in java inside java.util
class Abc {
    public static void main(String args[]) throws Exception {             
        String[] Answers = new String[4];
        Answers[0] = "OS";
        Answers[1] = "GOOD";
        Answers[2] = "CS";
        Answers[3] = "Cody";  
        Random rn = new Random();
        int freq[]=new int[Answers.length];
        for(int n=0;n<Answers.length;n++) {
            int index = rn.nextInt(Answers.length);
            if(freq[index]==0) {
                freq[index]+=1;
                System.out.println(Answers[index]);       
            } else {
                n-=1;   
            }
        }
    }
}
Chirag
  • 555
  • 1
  • 5
  • 20
ritwikshanker
  • 456
  • 7
  • 19
1

Arrays Class has multiple utilities methods

String[] Answers = new String[4];
    Answers[0] = "OS";
    Answers[1] = "GOOD";
    Answers[2] = "CS";
    Answers[3] = "Cody";
    System.out.println(Arrays.toString(Answers));
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

Don't use Random as your class name as it predefined java class name. Use Random class for getting random integer.

public class RandomStringExample{
    public static void main(String args[]) throws Exception {             
        String[] Answers = new String[4];
        Answers[0] = "OS";
        Answers[1] = "GOOD";
        Answers[2] = "CS";
        Answers[3] = "Cody";
        System.out.println(getRandomString(Answers));
    }

    public static String getRandomString(String[] answers) {
        Random random = new Random();
        int n = random.nextInt(answers.length-1);
        return answers[n];
    }
}  
Drizzle
  • 15
  • 2
  • 4
Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18
1

Using Random you can do it like the following:

public class Test
{
    public static void main(String args[]) throws Exception
    {
        String[] answersArray = new String[4];
        answersArray[0] = "OS";
        answersArray[1] = "GOOD";
        answersArray[2] = "CS";
        answersArray[3] = "Cody";

        List<String> answersList = new ArrayList<String>(Arrays.asList(answersArray));

        final Random randomNumber = new Random();
        final int low = 0;
        int high = 0;

        while(!answersList.isEmpty()){
            high = answersList.size();
            int result = randomNumber.nextInt(high - low) + low;
            System.out.print(answersList.get(result) + " ");
            answersList.remove(result);
        }
    }
}
Karan Khanna
  • 1,947
  • 3
  • 21
  • 49
1

Try this

public static void main(String args[]) throws Exception {
    String[] answers = new String[4];
    int len = answers.length;
    answers[0] = "OS";
    answers[1] = "GOOD";
    answers[2] = "CS";
    answers[3] = "Cody";
    int random = getRandomNum(len);
    System.out.println("random: " + random);
    System.out.println(answers[random]);
}

private static int getRandomNum(int max) {
    int rand = (int) (Math.random() * 100);
    if (rand < max)
        return rand;
    return getRandomNum(max);
}

Output

Run 1

random: 3
Cody

Run 2

random: 1
GOOD

Update if have to print all elements randomly in one run then this will helps

public static void main(String args[]) throws Exception {
    String[] answers = new String[4];
    int len = answers.length;
    answers[0] = "OS";
    answers[1] = "GOOD";
    answers[2] = "CS";
    answers[3] = "Cody";
    for (int i = 0; i < len; i++) {
        int random = getRandomNum(len);
        System.out.println("random: " + random);
        System.out.println(answers[random]);
    }
}

private static int getRandomNum(int max) {
    int rand = (int) (Math.random() * 100);
    if (rand < max)
        return rand;
    return getRandomNum(max);
}

Output

random: 1
GOOD
random: 3
Cody
random: 0
OS
random: 2
CS

Update If you don't want any repeated values in your output, then head over to this simple and short method

public static void main(String args[]) throws Exception {
    String[] answers = new String[4];
    answers[0] = "OS";
    answers[1] = "GOOD";
    answers[2] = "CS";
    answers[3] = "Cody";

    List<String> list = Arrays.asList(answers);
    System.out.println("Before: " + list.toString());
    Collections.shuffle(list);
    System.out.println("After: " + list.toString());
}

Output

Before: [OS, GOOD, CS, Cody]
After: [Cody, OS, GOOD, CS]

Hope this helps :)

Chirag
  • 555
  • 1
  • 5
  • 20
  • I think he require to print all the elements and not one in one run. – Karan Khanna Jun 28 '18 at 11:33
  • The updated code have a probability of printing the same element multiple times. Please refer my answer. It eliminates that probability. – Karan Khanna Jun 28 '18 at 11:45
  • The question says that print string array randomly, means any value can repeat as we can not determine what is the next random. and yes there is probability to print same elements in random mode. – Chirag Jun 28 '18 at 11:58
  • With what he have mentioned in the desired output it feels like he don't want to get the values repeated. – Karan Khanna Jun 28 '18 at 12:00
  • Now this one is very lightweight solution and does not eats up memory, and there is no chance or probability of printing one elements more times. – Chirag Jun 28 '18 at 12:14
  • Nice one.! This looks good. – Karan Khanna Jun 28 '18 at 12:16
0

you should provide array index with name.

like this:

System.out.print(Answers[n]);
john cena
  • 191
  • 1
  • 1
  • 7
0

First you need to change your print statement

String[] Answers = new String[4];
Answers[0] = "OS";
Answers[1] = "GOOD";
Answers[2] = "CS";
Answers[3] = "Cody";  

for(int n=0;n<Answers.length;n++) {
    System.out.print(Answers[n].toString() + " " );              
}

Then you'll want to use a random number generator to pick a random number between 1 and 4 to print from yours Answers array

Drizzle
  • 15
  • 2
  • 4
Christopher
  • 66
  • 13