简体   繁体   English

从Java中某个文件的某个随机行扫描?

[英]Scanning from a certain, random line of a file in java?

I have a .txt file that lists integers in groups like so: 我有一个.txt文件,该文件在组中列出了整数,如下所示:

20,15,10,1,2 20,15,10,1,2

7,8,9,22,23 7,8,9,22,23

11,12,13,9,14 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. 我能想到的唯一方法是在while循环中增加一个变量,该变量将为我提供行数,然后以某种方式从随机选择的那些行之一中读取内容,但我不确定它的读取方式仅从这些行之一中随机选择。 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? 现在我该怎么做才能使其扫描行r并将行r上读取的所有整数放入一维数组中?

I'm assuming you know how to parse the line and get the integers out ( Integer.parseInt , perhaps with a regular expression). 我假设您知道如何解析该行并获取整数( Integer.parseInt ,也许带有正则表达式)。 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"

There is an old answer in StackOverflow about choosing a line randomly. 在StackOverflow中有一个关于随机选择行的旧答案 By using the choose() method you can randomly get any line. 通过使用choose()方法,您可以随机获取任何行。 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]);

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
    //...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM