0

I am fairly new to java, so please bear with me if I have done something wrong. I have written a code in java that reads in N number of lines from a file in java and puts it in array of double and then prints it out;

ArrayList<Double> numbers = new ArrayList<Double>();
Scanner read = new Scanner(new File("numberfile"));
int counter = 0;
while(read.hasNextLine() && counter < 10)
{
    System.out.println(read);
    counter++;
}

The file contains bunch of numbers from 1 to 100;

Currently, my code prints out all the numbers like this [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], if I tell it to read the first 10 numbers. What I want to do now is print out these numbers in a random order, for example [2, 1, 6, 8, 9, 3, 7, 10, 3, 5].

And also if possible, I want to write a code that prints out the first 10 numbers randomly N number of times. For example, print out the first 10 numbers 50 times in a random order.

Thanks for your help and please let me know if I am unclear.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Henry
  • 1,042
  • 2
  • 17
  • 47
  • I suggest you look at collections (e.g. `ArrayList`) and `Collections.shuffle`. – Jon Skeet Dec 31 '14 at 17:44
  • For the random output you should see the answer [How to randomize ArrayList](http://stackoverflow.com/questions/4228975/how-to-randomize-arraylist) and if that doesn't suit just lookup "java arraylist shuffle" or "java arraylist randomisze". For the others you store it so why don't you just have an additional variable that, after the while, uses that variable to print the `numbers` list that many times? – scrappedcola Dec 31 '14 at 17:45
  • I managed to use Collection.shuffle to print it out randomly. Now is it possible to make it print out the first 10 numbers like 50 times in a random order? – Henry Dec 31 '14 at 17:52
  • Unrelated, you can replace your `while` loop with a `for` loop with: `for (int counter = 0; read.hasNextLine() && counter < 10; counter++)` – Greg Potter Dec 31 '14 at 17:52

4 Answers4

0

You can put them into a List and then use Collections.shuffle method.

kraskevich
  • 18,368
  • 4
  • 33
  • 45
0

Read your numbers into an array, and java.util.Random to access your array and print as you wish (use a nested loop for printing 'x' number of times per access). If you just want to print randomly, you can use Collections.shuffle to shuffle and then simply iterate through the structure and print.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • I managed to use Collection.shuffle to print it out randomly. Now is it possible to make it print out the first 10 numbers like 50 times in a random order? – Henry Dec 31 '14 at 17:56
0

Well, if your file is small enough, using Java 7 it's easy:

final Path thefile = Paths.get("whereyourfileis");

final List<String> lines = Files.readAllLines(thefile, StandardCharsets.UTF_8);

Collections.shuffle(lines);

// lines.sublist(0, 10) --> done
fge
  • 119,121
  • 33
  • 254
  • 329
  • I managed to use Collection.shuffle to print it out randomly. Now is it possible to make it print out the first 10 numbers like 50 times in a random order? – Henry Dec 31 '14 at 17:54
  • Sorry, I don't understand what you mean at all? – fge Dec 31 '14 at 17:55
  • Basically, read the first 10 numbers in a random order like 50 times. for example the first 10 numbers are 1, 2, 3, 4, 5, 6, 7, 8 9, 10. I want to read these numbers 50 times in random order e.g. 1, 4, 8 , 10, 3, 1, 4, 5, 7, 3, 10, 10, 3, 5, 6 ..... 5, 6, 2, 1 – Henry Dec 31 '14 at 17:58
0

You should store the numbers as you read the file into an array (or list) and then either A) shuffle the array and print it out, or B) randomly select numbers from the array. If you don't care about repeated numbers (for example, [2, 1, 6, 1, 1, 1, 2]) you can just pick 10 items random using Math.Random(). Otherwise, read into a List as follows (you already have an ArrayList called numbers):

while(read.hasNextLine() && counter < 10)
{
    numbers.add(read.nextDouble());
    counter++;
}
for (int n = 0; n < 50; n++) {
    Collections.shuffle(numbers);
    for (int i = 0; i < numbers.size(); i++) {
        System.out.println(numbers.get(i));
    }
    System.out.println();
}
Greg Potter
  • 527
  • 1
  • 5
  • 18
  • I managed to use Collection.shuffle to print it out randomly. Now is it possible to make it print out the first 10 numbers like 50 times in a random order? – Henry Dec 31 '14 at 17:54
  • Yes, that's what the outer `for` loop is for. Just repalce NUM_SHUFFLES with 50. – Greg Potter Dec 31 '14 at 17:55
  • Put an extra System.out.println(); right before the final `}`. I've edited my post to reflect it. You should be able to see them grouped together now. If you need them formatted differently then you can change the inner println to `System.out.print(numbers.get(i) + " "));` or something similar. – Greg Potter Dec 31 '14 at 18:07
  • Hi, sorry, it still isn't working. Now it prints 15 numbers all the time and there are not random. – Henry Dec 31 '14 at 18:22
  • I just updated my answer (it had a few typos) and tested it with a file of numbers and it seemed to work fine. You should be able to just copy and paste the whole thing. – Greg Potter Dec 31 '14 at 18:29
  • worked, thanks. I had to make few changes. Thanks for your help. I have marked your answer as correct but I won't be able to vote up because of low rep – Henry Dec 31 '14 at 18:47