AKIWEB
AKIWEB

Reputation: 19612

Reading Line in a random way, then print numbers in random order

Below is the text file, in which I want to read each line in some random way by printing each number in the line in some random order. I can read each line one by one and then print number corresponding to each line in a sequential order, But is there any way we can read line in some random way so that I can print all the numbers in some random order.

 Line1   1  1116    2090    100234  145106  76523
 Line2   1  10107   1008    10187
 Line3   1  10107   10908   1109

Any suggestions will be appreciated. Below is the code that I wrote it will read the line sequentially.

BufferedReader br = null;

    try {
        String sCurrentLine;

        br = new BufferedReader(new FileReader("C:\\testing\\Test.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            String[] s = sCurrentLine.split("\\s+");
            for (String split : s) {
                if(split.matches("\\d*"))
                System.out.println(split);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

Upvotes: 1

Views: 216

Answers (3)

maybeWeCouldStealAVan
maybeWeCouldStealAVan

Reputation: 15610

If you mean to rearrange the order of each line, you can use Collections.shuffle:

while ((sCurrentLine = br.readLine()) != null) {
    List<String> s = Arrays.asList(sCurrentLine.split("\\s+"));
    Collections.shuffle(s);
    for (String split : s) {
        if (split.matches("\\d*")) {
            System.out.println(split);
        }
    }
}

This will print the lines sequentially, but the numbers in each line will be shuffled.

If mean to shuffle the order of the lines as well, just add each line to an ArrayList<List<String>>, shuffle the ArrayList, then shuffle each line:

ArrayList<List<String>> allLines = new ArrayList<List<String>>();
while ((sCurrentLine = br.readLine()) != null) {
    allLines.add(Arrays.asList(sCurrentLine.split("\\s+")));
    Collections.shuffle(allLines);
    for (List<String> s : allLines) {
        Collections.shuffle(s);
        for (String split : s) {
            if(split.matches("\\d*"))
            System.out.println(split);
        }
    }
}

Upvotes: 1

John3136
John3136

Reputation: 29265

You can't "read a line in some random way" (well, you could, but it would be horrid!)

I'd suggest reading all your lines sequentially into a collection and then pick them out 1 at a time (randomly) until the collection is empty.

You can process each line individually in a similar way: parse all the numbers into a collection then pull them back out randomly.

e.g. (pseudo code)

ArrayList lines = new ArrayList()
while (! EOF)
    lines.append(readLine)

while(lines.size() > 0)
    int index = Random(0, lines.size)
    line = lines[index];
    lines.remove(index)
    processLine(line)
    // processLine does a similar thing to the above but with numbers
    // on a line rather than lines in a file.

Upvotes: 4

user1067001
user1067001

Reputation:

store your spilt variable into some arraylist or array of the type you want to have a collection of the numbers in your text file

Upvotes: 0

Related Questions