简体   繁体   English

使用扫描仪读取文件和跳过行时出现问题

[英]Troubles while reading file with Scanner and skip lines

I need to read the following txt.file with Scanner in java.我需要使用 java 中的Scanner读取以下 txt.file。 The problem with this file is that there is a lot of information I don't need.这个文件的问题是有很多我不需要的信息。 The only things I need are the parameter(s) and the values of those parameters ( as an example, I need String n and int 5, String p and double 0.5, String lambda and double 0.3 ... ) the problem seems to be in the empty lines.我唯一需要的是参数和这些参数的值(例如,我需要 String n 和 int 5、String p 和 double 0.5、String lambda 和 double 0.3 ...)问题似乎是在空行中。 I added the code I made, but if I run this, the second line of a distribution is never read.我添加了我制作的代码,但是如果我运行它,则永远不会读取发行版的第二行。

What am I doing wrong?我究竟做错了什么?

txt-file as input: 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

code代码

for(int a = 0; a< 6; ++a){

 inputStream.nextLine();
 System.out.print("\n"+inputStream.next());
 System.out.print("\n"+inputStream.next());
 String line = "";
 if (!(line = inputStream.nextLine()).isEmpty()) 
    {
     System.out.print("\n"+inputStream.next());
     System.out.print("\n"+inputStream.next());
    }
 else
 {

 }
 inputStream.nextLine();
 }}

Here are some improvements:以下是一些改进:

  • For the loop header I'd use a++ to increment a -- just preference.对于循环头,我会使用a++来增加a -- 只是偏好。
  • .nextLine() returns a String so maybe reset the inputStream variable after executing .nextLine() . .nextLine()返回一个String因此可能会在执行.nextLine()后重置inputStream变量。
  • Print inputStream.nextLine() rather than inputStream.next() .打印inputStream.nextLine()而不是inputStream.next()
  • In the if conditional I wouldn't initialise the variable line .if条件下,我不会初始化变量line
  • The if else condition doesn't need to be there to do what you want, ie to read the six values. if else条件不需要在那里做你想做的事情,即读取六个值。

My recommendation is to read up about REGEX and if the REGEX of the line matches n,p,lamda,d and another value, then print.我的建议是阅读有关REGEX ,如果该行的REGEXn,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());
}

Short answer: Don't use Scanner .简短回答:不要使用Scanner

You said you "would like to read the following txt.file with the scanner method", but you didn't say why, and we don't always get what we like.你说你“想用扫描仪的方法阅读下面的txt.file”,但你没有说为什么,我们并不总是得到我们喜欢的。 In this case, using a Scanner is far from the best choice.在这种情况下,使用Scanner远不是最佳选择。

Looking at the file, the format seems to be data in blocks of 3 lines, with first line starting with the word distributie .查看文件,格式似乎是 3 行块中的数据,第一行以单词distributie开头。 At the end of the file there's a summary line starting with aantal .在文件的末尾有一个以aantal开头的摘要行。

Lines 2 and 3 of each block is either a keyword + decimal number, or a blank line.每个块的第 2 行和第 3 行要么是关键字 + 十进制数,要么是空行。 Since you are only after those keyword + number pairs, I'd suggest reading the file line-by-line, and matching keyword + number lines using a regular expression:由于您只在那些关键字 + 数字对之后,我建议逐行阅读文件,并使用正则表达式匹配关键字 + 数字行:

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);
        }
    }
}

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

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