0

I'm just learning Java and I'm having a problem with ArrayList or retrieving a random line of text. What I'm trying to achieve is a random quote (from a list) to load on every page refresh. Never done an Array/Random before, not sure if this is the right method?

Here's what's working so far: https://jsfiddle.net/5wryh23L/

code that's not working:

 Random random = new Random(); 
 int index = random.nextInt(10);


    List<String> list = new ArrayList<String>();
    list.add("quote1");
    list.add("quote2");
    list.add("quote3");
    list.add("quote4");
    list.add("quote5");

    RandomExample obj = new RandomExample();
    for(int i = 0; i < 10; i++){
        System.out.println(obj.getRandomList(list));
    }

}

getRandomList(List<String> list) {

    //0-4
    int index = random.nextInt(list.size());
    System.out.println("\nIndex :" + index );
    return list.get(index);

}
12
  • What's the issue? Are you getting unexpected output, or an error? Commented Jul 2, 2015 at 18:47
  • what's on jsfiddle is working the way I want it to. The issue is I'm not quite sure how to properly add in an array list to load random quotes. Nothing loads with the code above. Commented Jul 2, 2015 at 18:52
  • Can I see the whole Java code? Commented Jul 2, 2015 at 18:59
  • java code w/out array: jsfiddle.net/5wryh23L Commented Jul 2, 2015 at 19:10
  • 1
    Look into what programming languages are, and what makes them different. Commented Jul 2, 2015 at 19:41

3 Answers 3

2

A really easy way to get a random item from your list:

list.get((int)(Math.random()*list.size()));
Sign up to request clarification or add additional context in comments.

Comments

0
    This example gives how to shuffle elements in the ArrayList. By calling Collections.shuffle() method you can shuffle the content of the ArrayList. Every time you call shuffle() method, it generates different order of output. 

        **Example #1**

            public class MyListShuffle {

                public static void main(String a[]){
                    ArrayList<String> wordList = new ArrayList<String>();
                    list.add("Stack");
                    list.add("Overflow");
                    list.add("is");
                    list.add("a");
                    list.add("place");
                    list.add("great");
                    list.add("to";
                    list.add("learn");
                    list.add("and");
                    list.add("teach");



                    Collections.shuffle(wordList);
                    System.out.println("Results after shuffle operation #1:");
                    for(String word: wordList){
                        System.out.println(word);
                    }


                    Collections.shuffle(wordList);
                    System.out.println("Results after shuffle operation #2:");
                    for(String word: wordList){
                        System.out.println(word);
                    }
                }
            }

        Note that The shuffle(List<?>) method is used to randomly permute the specified list using a default source of randomness.

    The original declaration:
    -  java.util.Collections.shuffle() method.  
         - public static void shuffle(List<?> list)
            - Parameter: "list" is the list to be shuffled.

Output:
Results after shuffle operation #1:
to 
learn
and
Stack
Overflow
a
teach 
is
great
place
Results after shuffle operation #2:
Overflow
place
great 
and
teach
to
is
learn
Stack
a

Example #2

  public class ShuffleList {
           public static void main(String args[]) {
           // create an array list object       
           List myList = new ArrayList();

           // populate the list
           myList.add("A");
           myList.add("B");
           myList.add("C"); 
           myList.add("D");
           myList.add("E"); 

           System.out.println("Collection after: "+ myList);

           // shuffle the list
           Collections.shuffle(myList);

           System.out.println("Collection after shuffle: "+ myList);
           }    
        } 

After compiling the program, the output is:

Collection before shuffle: [A, B, C, D, E]
Collection after shuffle: [C, A, E, B, D]

Or you can return as following

Example #3

    return list.get(random.nextInt(list.size()));

2 Comments

Looks like OP's not interested in shuffle, rather he/she needs one random String out the list to display up on refresh.
The example #1 is one random String out of the the list as output. I am giving here different examples for further learning.
0

Your random methods is apparently correct and the way you are structuring your list also. I believe your problem is because you don't have a main method do you? If you create the list inside a main method and call the getRandomList() inside the main method it will works. It will be more or less like this:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


static Random random = new Random(); 
    int index = random.nextInt(10);

    public static void main(String args[]){
        List<String> list = new ArrayList<String>();
        list.add("quote1");
        list.add("quote2");
        list.add("quote3");
        list.add("quote4");
        list.add("quote5");

        // RandomExample obj = new RandomExample
        for(int i = 0; i < 10; i++){
            System.out.println(getRandomList(list));
        }

    }



    static String getRandomList(List<String> list) {

        //0-4
        int index = random.nextInt(list.size());
        System.out.println("Index :" + index );
        return list.get(index);

    }

2 Comments

@Tuco..what's the purpose of field int index?
@ringbearer In that case...none, since the getRandom is using the local parameter list.size to get the index. I just left it because it was on the original code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.