0

I have an input which contains one symbol on each line and have used scanner to read into an arralist (maybe). I would like to have 1000 subsets of data randomly from this arraylist (size=3) and save it into a file line by line(each line is a subset).

So the input is like :

AAA BBB CCC DDD EEE FFF 

Expected output would be:

AAA EEE FFF CCC FFF BBB DDD BBB AAA 

Here is what I have so far. I'm kind of stucking on how to indicate 1000 time of shuffling and how to define size=3. I expect if I can shuffle 1000 times and pick up first 3 on each. It is my strategy.

public class sampling { public static void main(String[] args) throws FileNotFoundException{ Scanner scan1 = new Scanner(new File("Symbol.txt")); ArrayList<String> Wholelist = new ArrayList<>(); while (scan1.hasNextLine()){ String line = scan1.nextLine(); Wholelist.add(line); } try{ FileWriter stream = new FileWriter(args[2]); BufferedWriter out = new BufferedWriter(stream); for (int i=0,n=Wholelist.size();i<n; i++){ Collections.shuffle(Wholelist); Wholelist.get(3); } } catch (IOException e) { System.err.println("Error: "+ e.getMessage()); } } } 

Please try to modify it based on my code because I'm a beginner of Java. Thank you so much if anyone can help.

2
  • thanks @tobias_k, I was trying to edit. Commented Jun 30, 2014 at 10:36
  • 1
    why shuffle, just iterate over your input list 1000 times, to randomize it over each iteration pick any three random list indices in each iteration and get corresponding elements. Commented Jun 30, 2014 at 10:44

3 Answers 3

1

You are on the right track. Just iterate with your loop 1000 times instead of up to n! And to get the first three elements of a list use the method subList(0, 3)

Sign up to request clarification or add additional context in comments.

Comments

1

try something similar to suit your requirements:

public static void main(String[] args) { List<String> wholeList = new ArrayList<>(); wholeList.add("AAA"); wholeList.add("BBB"); wholeList.add("CCC"); wholeList.add("DDD"); wholeList.add("EEE"); wholeList.add("FFF"); wholeList.add("GGG"); int randomLimit = wholeList.size(); Random r = new Random(); for (int i = 0; i < 1000; i++) { // get three random indices from 0(inclusive) to list max size // (exclusive) int u = r.nextInt(randomLimit); int v = r.nextInt(randomLimit); int w = r.nextInt(randomLimit); // chose your format System.out.print(wholeList.get(u) + "\t"); System.out.print(wholeList.get(v) + "\t"); System.out.println(wholeList.get(w)); } } 

Comments

1

Try this

 import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import java.util.Scanner; public class FileTester { public static void main(String[] args) throws FileNotFoundException { Scanner scan1 = new Scanner(new File("YOUR_INPUT_FILE")); ArrayList<String> wholeList = new ArrayList<String>(); String line = ""; while (scan1.hasNextLine()) { line = scan1.nextLine(); wholeList.add( line ); } try { FileWriter stream = new FileWriter("YOUR_OUTPUT_FILE"); BufferedWriter out = new BufferedWriter(stream); for (int i = 0, n = wholeList.size(); i < n; i++) { Random r = new Random(); Collections.shuffle(wholeList); out.append(wholeList.get( r.nextInt(wholeList.size()) ) + "\t"); out.append(wholeList.get( r.nextInt(wholeList.size()) ) + "\t"); out.append(wholeList.get( r.nextInt(wholeList.size()) ) + "\t"); out.append("\n"); } out.close(); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } } } 

1 Comment

any advantage of using shuffle, how is it increasing the probability to pick a new element in the list? How is it different from just shuffle and pick fixed indices, say first three?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.