Java 使用扫描仪和跳过行读取文件时出现问题
我需要用java中的Java 使用扫描仪和跳过行读取文件时出现问题,java,Java,我需要用java中的Scanner读取以下txt.file。这个文件的问题是有很多我不需要的信息。我只需要参数和这些参数的值(例如,我需要字符串n和int 5,字符串p和double 0.5,字符串lambda和double 0.3…),问题似乎在空行中。我添加了我编写的代码,但是如果我运行它,发行版的第二行永远不会被读取 我做错了什么 作为输入的txt文件: distributie lengte klasse 1 naam verdeling parameter(s) value of
Scanner
读取以下txt.file。这个文件的问题是有很多我不需要的信息。我只需要参数和这些参数的值(例如,我需要字符串n和int 5,字符串p和double 0.5,字符串lambda和double 0.3…),问题似乎在空行中。我添加了我编写的代码,但是如果我运行它,发行版的第二行永远不会被读取
我做错了什么
作为输入的txt文件:
distributie lengte klasse 1 naam verdeling parameter(s) value of parameter
n 5
p 0.5
distributie lengte klasse 2 naam verdeling parameter(s) value of parameter
lambda 0.3
distributie incidentie klasse 1 naam verdeling parameter(s) value of parameter
d 1
distributie incidentie klasse 2 naam verdeling parameter(s) value of parameter
n 8
p 0.1
distributie servertijd klasse 1 naam verdeling parameter(s) value of parameter
d 1
distributie servertijd klasse 2 naam verdeling parameter(s) value of parameter
p 0.3
aantal pakketten te verwerken 2000000
代码
for(int a=0;a<6;++a){
inputStream.nextLine();
System.out.print(“\n”+inputStream.next());
System.out.print(“\n”+inputStream.next());
字符串行=”;
if(!(line=inputStream.nextLine()).isEmpty())
{
System.out.print(“\n”+inputStream.next());
System.out.print(“\n”+inputStream.next());
}
其他的
{
}
inputStream.nextLine();
}}
以下是一些改进:
- 对于循环头,我将使用
来增加a++
——只是首选项a
.nextLine()
- 打印
而不是inputStream.nextLine()
inputStream.next()
- 在
条件中,我不会初始化变量if
行
条件不需要在那里执行您想要的操作,即读取六个值if-else
REGEX
的内容,如果行的REGEX
与n、p、lamda、d
和另一个值匹配,则打印
for(int a = 0; a< 6; a++){
inputStream.nextLine();
//possibly reset inputStream here with something like inputStream = new Scanner(...);
System.out.print("\n"+inputStream.nextLine());
System.out.print("\n"+inputStream.nextLine());
}
for(int a=0;a<6;a++){
inputStream.nextLine();
//可能在这里重置inputStream,类似于inputStream=new Scanner(…);
System.out.print(“\n”+inputStream.nextLine());
System.out.print(“\n”+inputStream.nextLine());
}
简短回答:不要使用扫描仪
你说你“想用scanner方法读取下面的txt.file”,但你没有说为什么,我们也不总是能得到我们想要的。在这种情况下,使用扫描仪
远非最佳选择
查看该文件,格式似乎是3行数据块,第一行以单词distributive
开头。文件末尾有一个摘要行,以aantal
开头
每个块的第2行和第3行要么是关键字+十进制数,要么是空行。由于您只关注那些关键字+数字对,我建议逐行阅读文件,并使用正则表达式匹配关键字+数字行:
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
Pattern p = Pattern.compile("\\s+(\\w+)\\s+([0-9.]+)\\s*");
for (String line; (line = in.readLine()) != null; ) {
Matcher m = p.matcher(line);
if (m.matches()) {
String keyword = m.group(1);
double number = Double.parseDouble(m.group(2));
System.out.println(keyword + " = " + number);
}
}
}
可能只是一个提示的重复:您不需要
else{}
。如果您没有在If
的部分中执行任何操作,则可以避免放置它。感谢您的帮助,我从未使用过正则表达式,但我将尝试使用它。。。如果我使用这个,只会找到一个关键字和数字。我是否必须在它周围创建另一个循环,使它遍历整个txt.file?对不起,模式的最后一个字符应该是*
,而不是+
。
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
Pattern p = Pattern.compile("\\s+(\\w+)\\s+([0-9.]+)\\s*");
for (String line; (line = in.readLine()) != null; ) {
Matcher m = p.matcher(line);
if (m.matches()) {
String keyword = m.group(1);
double number = Double.parseDouble(m.group(2));
System.out.println(keyword + " = " + number);
}
}
}