简体   繁体   English

使用扫描仪跳过特定行

[英]Skip a specific line using scanner

I wrote this code to read a text file and I am running into a problem where it skips the 4th line rather than a line that meets the condition.我写了这段代码来读取一个文本文件,但我遇到了一个问题,它跳过了第 4 行而不是满足条件的行。

Here's my code:这是我的代码:

    while(fileReader.hasNext()) {
                        String label = fileReader.nextLine();
                        String prompt = fileReader.nextLine();
                        String message = fileReader.nextLine();
                        
                        if(fileReader.nextLine().contains(label)) {
                            fileReader.nextLine();
                        }
                        
                        System.out.println(label);
                        System.out.println(prompt);
                        System.out.println(message);
    }           

The prompt that I am trying to read is this.我正在尝试阅读的提示是这样的。 In this case, I want to skip every line that contains information about the number of children a node has.在这种情况下,我想跳过包含有关节点拥有的子节点数量信息的每一行。 (Line 4(root 3) and 14(1 3)) (第 4 行(root 3)和 14(1 3))

root                                       // Will always be "root"
Root Node                                  // Will never be displayed to screen
What Model is the Washing Machine?         // Message
root 3                                     // Number of children for label root: 3
1                                          // First child. Label
WM200                                      // First child. Prompt
What is the problem?                       // First child. Message
2                                          // Second child. Label
WM300                                      // Second child. Prompt
What is the problem?                       // Second child. Message
3                                          // Third child. Label
WM400                                      // Third child. Prompt
What is the problem?                       // Third child. Message
1 3                                        // Number of children for label 1: 3
1-1                                        // Label
No Water.                                  // Prompt
Is the hose attached?                      // Message, etc.

Desired Output:所需 Output:

root                                       // Will always be "root"
Root Node                                  // Will never be displayed to screen
What Model is the Washing Machine?         // Message
1                                          // First child. Label
WM200                                      // First child. Prompt
What is the problem?                       // First child. Message
2                                          // Second child. Label
WM300                                      // Second child. Prompt
What is the problem?                       // Second child. Message
3                                          // Third child. Label
WM400                                      // Third child. Prompt
What is the problem?                       // Third child. Message
1-1                                        // Label
No Water.                                  // Prompt
Is the hose attached?                      // Message, etc.

And I am getting the following output from my code:我从我的代码中得到以下 output:

root                                     
Root Node                                 
What Model is the Washing Machine?         
1                                           
WM200                                       
What is the problem?                        
WM300                                       
What is the problem?                       
3                                          
What is the problem?                        
1 3                                         
1-1                                         
Is the hose attached?    

So my code ends up skipping every fourth line rather than only those lines that meet the condition.所以我的代码最终每四行跳过一次,而不仅仅是那些满足条件的行。

You read the line in the if condition without storing it you should use:您在if条件下读取了该行而不存储它,您应该使用:

while (fileReader.hasNext()) {
    String label = fileReader.nextLine();
    String prompt = fileReader.nextLine();
    String message = fileReader.nextLine();

    String line = fileReader.nextLine();
    if (line.contains(label)) {
        line = fileReader.nextLine();
    }

    // continue your logic with the stored line
    System.out.println(label);
    System.out.println(prompt);
    System.out.println(message);
}     

  

So you are trying to rebuild a Tree which has been persisted (breadth-first) into this text.因此,您正在尝试重建一棵已保存(广度优先)到本文中的树。 The question you ask is not the correct one as the data is not being read correctly.您问的问题不正确,因为数据读取不正确。 Looking at the text and your comment...看着文字和你的评论......

 1 3                                        // Number of children for label 1: 3

Clearly this is nothing to do with the label of node 3 appearing a few lines before.显然这与之前几行出现的节点3的label无关。 Your data format is:您的数据格式是:

  • Node uses 3 lines only节点仅使用3 行
  • Children block starts with "parent count" followed by count Node s.块以“parent count”开头,后跟count Node s。

Try this:试试这个:

    Pattern regex = Pattern.compile("^(\\S+) (\\d+)"); // Header is "<parent-label> <count>"
    String parent = null;
    
    while (fileReader.hasNext()) {
      String header = fileReader.nextLine(); // new stanza - could be Node or Children
      Matcher m = regex.matcher(header);
      if (m.find()) {
        // Handle Children
        parent = m.group(1);
      } else {
        // Handle Node
        String label = header;
        String prompt = fileReader.nextLine();
        String message = fileReader.nextLine();

        System.out.println(String.format("%s/%s %s %s", parent, label, prompt, message));
      }
    }

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

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