跳过特定行使用扫描器

huangapple go评论36阅读模式
英文:

Skip a specific line using scanner

问题

以下是您的代码的翻译:

我编写了这段代码来读取文本文件但遇到了一个问题它跳过了第4行而不是符合条件的行

这是我的代码

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

}


我尝试读取的提示如下。在这种情况下,我想跳过包含有关节点子代数量信息的每一行(第4行(root 3)和第14行(1 3))。

root // 总是为“root”
Root Node // 永远不会显示在屏幕上
What Model is the Washing Machine? // 消息
root 3 // 标签root的子代数量:3
1 // 第一个子代。标签
WM200 // 第一个子代。提示
What is the problem? // 第一个子代。消息
2 // 第二个子代。标签
WM300 // 第二个子代。提示
What is the problem? // 第二个子代。消息
3 // 第三个子代。标签
WM400 // 第三个子代。提示
What is the problem? // 第三个子代。消息
1 3 // 标签1的子代数量:3
1-1 // 标签
No Water. // 提示
Is the hose attached? // 消息,等等。


期望的输出:

root // 总是为“root”
Root Node // 永远不会显示在屏幕上
What Model is the Washing Machine? // 消息
1 // 第一个子代。标签
WM200 // 第一个子代。提示
What is the problem? // 第一个子代。消息
2 // 第二个子代。标签
WM300 // 第二个子代。提示
What is the problem? // 第二个子代。消息
3 // 第三个子代。标签
WM400 // 第三个子代。提示
What is the problem? // 第三个子代。消息
1-1 // 标签
No Water. // 提示
Is the hose attached? // 消息,等等。


但是,我从我的代码中获得以下输出:

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?


因此,我的代码最终跳过了每四行,而不是仅跳过满足条件的行。

<details>
<summary>英文:</summary>

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.

Here&#39;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)) 

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: 

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: 

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.


</details>


# 答案1
**得分**: 2

你在`if`条件中读取了一行而没有存储它,
你应该使用:

```java
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();
    }

    // 使用存储的行继续你的逻辑
    System.out.println(label);
    System.out.println(prompt);
    System.out.println(message);
}
英文:

You read the line in the if condition without storing it
you should use:

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

答案2

得分: 0

以下是您要翻译的部分:

所以你正在尝试重建一个已经以广度优先方式持久化到这个文本中的树。你提出的问题并不正确,因为数据没有被正确读取。看一下文本和你的评论...

1 3 // 标签1的子节点数量:3

显然,这与节点3的标签出现在几行之前无关。您的数据格式如下:

  • 节点 仅使用 3行
  • 子节点 块以“父节点数量”开头,后跟 count 节点

尝试这个:

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));
  }
}
英文:

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. Your data format is:

  • Node uses 3 lines only
  • Children block starts with "parent count" followed by count Nodes.

Try this:

    Pattern regex = Pattern.compile(&quot;^(\\S+) (\\d+)&quot;); // Header is &quot;&lt;parent-label&gt; &lt;count&gt;&quot;
    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(&quot;%s/%s %s %s&quot;, parent, label, prompt, message));
      }
    }

huangapple
  • 本文由 发表于 2020年8月3日 06:44:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63221711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定