在java中,从文件的某个随机行扫描?
我有一个.txt文件,其中列出了分组中的整数,如下所示: 20,15,10,1,2 7,8,9,22,23 11,12,13,9,14 我想随机读取其中一个组,并将该组的整数存储到一个数组中。我该怎么做呢?每组有一行五个整数,用逗号分隔。我能想到的唯一方法是在while循环中增加一个变量,它会给我行数,然后以某种方式从随机选择的行中读取,但我不确定它如何仅从其中一行随机读取。下面是我能想出的一些代码来解释我的想法:在java中,从文件的某个随机行扫描?,java,file,Java,File,我有一个.txt文件,其中列出了分组中的整数,如下所示: 20,15,10,1,2 7,8,9,22,23 11,12,13,9,14 我想随机读取其中一个组,并将该组的整数存储到一个数组中。我该怎么做呢?每组有一行五个整数,用逗号分隔。我能想到的唯一方法是在while循环中增加一个变量,它会给我行数,然后以某种方式从随机选择的行中读取,但我不确定它如何仅从其中一行随机读取。下面是我能想出的一些代码来解释我的想法: int line = 0; Scanner filescan = new Sca
int line = 0;
Scanner filescan = new Scanner (new File("Coords.txt"));
while (filescan.hasNextLine())
{
line++;
}
Random r = new Random(line);
现在我该怎么做才能让它扫描r行,并将r行上读取的所有整数放入一个一维数组?我假设您知道如何解析该行并取出整数(
Integer.parseInt
,可能使用正则表达式)。如果您正在使用扫描仪,可以在构造函数中指定
保留每行的内容,并使用:
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"
int行=0;
Scanner filescan=new Scanner(新文件(“Coords.txt”);
列表内容=新建ArrayList();//刚出现的
while(filescan.hasNextLine())
{
content.add(filescan.next());//新建
line++;
}
随机r=新随机(线);
字符串编号=content.get(r.nextInt(content.size());//新建
//从“数字”中获取数字
有一个关于随机选择一行的方法。通过使用choose()
方法,你可以随机获得任何一行。我不相信答案。如果你喜欢我的答案,请投票
String[]numberLine=choose(新文件(“Coords.txt”).split(“,”);
int[]数字=新的int[5];
对于(int i=0;i<5;i++)
numbers[i]=Integer.parseInt(numberLine[i]);
从文件中逐个读取行,将它们存储在列表中,然后根据列表大小生成一个随机数,并使用它获取随机行
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
//...
}
publicstaticvoidmain(字符串[]args)引发异常{
列表列表=新的ArrayList();
Scanner filescan=new Scanner(新文件(“Coords.txt”);
while(filescan.hasNextLine()){
字符串nxtLn=filescan.nextLine();
//文件中可能有空行,请忽略它们
如果(!nxtLn.isEmpty()){
//在列表中添加行
列表添加(nxtLn);
}
}
System.out.println();
随机r=新随机();
int randomIndex=r.nextInt(aList.size());
//得到随机线
字符串行=aList.get(随机索引);
//制作一维阵列
//...
}
但是我如何将数字存储到数组中,而不是将数字行作为一个完整的字符串?我不确定如何将Integer.parseInt用于字符串中的多个数字。我是否必须导入某些内容或扩展类才能使用该方法?我不断收到一个错误,说该方法未定义用于类型测试。测试类的名称。复制在类中选择方法并使用它。您必须导入java.util.Scanner
。我刚刚意识到了这一点。谢谢!我现在就试用它。
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
//...
}