1

I have a .txt file that lists integers in groups like so:

20,15,10,1,2

7,8,9,22,23

11,12,13,9,14

and I want to read in one of those groups randomly and store the integers of that group into an array. How would I go about doing this? Every group has one line of five integers seperated by commas. The only way I could think of doing this is by incrementing a variable in a while loop that would give me the number of lines and then somehow read from one of those lines that is chosen randomly, but I'm not sure how it would read from only one of those lines randomly. Here's the code that I could come up with to sort of explain what I'm thinking:

int line = 0;
Scanner filescan = new Scanner (new File("Coords.txt"));
while (filescan.hasNextLine())
{ 
 line++;
}
Random r = new Random(line);

Now what do I do to make it scan line r and place all of the integers read on line r into a 1-d array?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Mdomin45
  • 469
  • 1
  • 6
  • 14

3 Answers3

0

I'm assuming you know how to parse the line and get the integers out (Integer.parseInt, perhaps with a regular expression). If you're sing a scanner, you can specify that in your constructor.

Keep the contents of each line, and use that:

int line = 0;
Scanner filescan = new Scanner (new File("Coords.txt"));
List<String> content = new ArrayList<String>(); // new
while (filescan.hasNextLine())
{ 
  content.add(filescan.next()); // new
  line++;
}
Random r = new Random(line);
String numbers = content.get(r.nextInt(content.size()); // new
// Get numbers out of "numbers"
ashes999
  • 9,925
  • 16
  • 73
  • 124
  • But how can I store the numbers into an array rather than just taking the line of numbers as a whole string? I'm not sure how to use Integer.parseInt for multiple numbers in a string. – Mdomin45 Dec 01 '13 at 20:52
0

There is an old answer in StackOverflow about choosing a line randomly. By using the choose() method you can randomly get any line. I take no credit of the answer. If you like my answer upvote the original answer.

String[] numberLine = choose(new File("Coords.txt")).split(",");
int[] numbers = new int[5];
for(int i = 0; i < 5; i++)
    numbers[i] = Integer.parseInt(numberLine[i]);
Community
  • 1
  • 1
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
0

Read lines one by one from the file, store them in a list and generate a random number from the list's size and use it to get the random line.

public static void main(String[] args) throws Exception {
    List<String> aList = new ArrayList<String>();
    Scanner filescan = new Scanner(new File("Coords.txt"));
    while (filescan.hasNextLine()) {
        String nxtLn = filescan.nextLine();
        //there can be empty lines in your file, ignore them
        if (!nxtLn.isEmpty()) {
            //add lines to the list
            aList.add(nxtLn);
        }
    }
    System.out.println();
    Random r = new Random();
    int randomIndex=r.nextInt(aList.size());
    //get the random line
    String line=aList.get(randomIndex);
    //make 1 d array
    //...

}
Nishant
  • 1,142
  • 1
  • 9
  • 27