mercredi 31 décembre 2014

How to read N number of lines from a file in java randomly


Vote count:

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.



asked 1 min ago







How to read N number of lines from a file in java randomly

Aucun commentaire:

Enregistrer un commentaire