【问题标题】:How to read the first element/term of a line in Java?如何读取 Java 中一行的第一个元素/术语?
【发布时间】:2017-04-03 08:40:47
【问题描述】:

我的目标是从给定的输入文件中读取每行的第一个元素/术语,然后根据第一个元素是什么来决定要做什么(使用if-else 构造)。 IE。如果第一个元素/单词恰好是"the"(如下面的代码中所述),那么我必须跳过该行并移至下一行。

到目前为止,我已经编写了以下代码,但我不确定如何仅读取作为输入传递的文本文件的每一行的第一个元素。

public static void main(String[] args) {

    BufferedReader br = null;
    try {
        String line;
        br = new BufferedReader(new FileReader("input.txt"));
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            StringTokenizer stringTokenizer = new StringTokenizer(line, " ");
            while (stringTokenizer.hasMoreTokens()) {

                String term  = stringTokenizer.nextElement().toString();
                if (term.equals("the")) {
                    //Code on what to do depending on the first character of each line. 
                }
                StringBuilder sb = new StringBuilder();
                System.out.println(sb.toString());    

            }

        } 

        System.out.println("Done!");

    }

    catch (IOException e)
    {
        e.printStackTrace();

    }

    finally {

        try {

           if (br != null)
               br.close();

        }

        catch(IOException ex) {

            ex.printStackTrace();

        }
    }

}

【问题讨论】:

  • 你搞定了吗?
  • 是的。它确实奏效了。谢谢。
  • @shashank2493, Yes. It did work 然后不要忘记通过单击复选标记“接受”激发您解决方案的答案,以便人们知道问题已解决。

标签: java string bufferedreader stringbuilder stringbuffer


【解决方案1】:

下面是打印the 作为输出的简单代码。您可以使用它,无需创建额外的数组或使用 StringTokenizer。

String s = "The a an the abcdef.";
System.out.println(s.contains(" ") ? s.substring(0, s.indexOf(" ")) : s);

【讨论】:

  • (1+) 很好的解决方案,因此您不会每次都自动标记整行。 (尽管如果一行中只有一个单词,它会遇到我提到的同样的问题)。
  • @camickr - 我错过了这一点。检查更新的条件。它肯定会解决这个问题。
  • 是的,这就是我要建议的。它得到了我的支持,因为我认为不需要标记整行来检查行中的第一个单词。
【解决方案2】:

您可以通过以下方式将每个术语转换为单词数组:

while((line = br.readLine()) != null){
    System.out.println(line);
    String word = line.split("\\s+")[0];
    if(word.equals("the")){
                //Code on what to do depending on the first character of each line. 
    }
    StringBuilder sb = new StringBuilder();
    System.out.println(sb.toString());    

}
...

【讨论】:

    【解决方案3】:

    但我不确定如何仅读取作为输入传递的文本文件的每一行的第一个元素。

    根据您的具体要求,有几种不同的解决方案。

    1. 您可以读取整行数据,然后使用String.startsWith(...) 方法测试第一个单词。如果您只想跳过该行的其余部分,则使用这种方法不会标记所有数据。然后,如果您想继续处理,可以使用String.substring(...) 方法从行中获取其余数据。

    2. 您可以使用Scanner 类。 Scanner 允许您在从文件中读取数据时对输入进行标记。所以你可以先读第一个单词,然后再决定是跳过其余数据还是读剩下的行。

    【讨论】:

    • 但是如果第一个单词是 theretheir 等,String.startsWith(...) 就不起作用。我想你错过了。
    • @SkrewEverything - 您在测试字符串中包含结尾空格。即startsWidth("the ")
    • 我想我错过了。
    • @SkrewEverything,虽然我猜每行上只能有一个标记,在这种情况下也行不通:) 正如我所说,解决方案取决于确切的要求。
    【解决方案4】:

    StringTokenizer 被视为遗留类。它只是为了向后兼容。在字符串上使用split() 将单个字符串拆分为字符串/单词数组。

    String[] s = line.readLine().split(" ");
    String firstWord = s[0]; // ->First word
    

    所以你的代码可以编辑成

    public static void main(String[] args) 
        {
    
            BufferedReader br = null;
    
            try
            {
                String line;
    
                br = new BufferedReader(new FileReader("input.txt"));
    
                while((line = br.readLine()) != null)
                {
    
                    System.out.println(line);
    
                    String s = line.split(" "); // instead of StringTokenizer
    
    
                        if(s[0].equals("the"))
                        {
                            //Code on what to do depending on the first character of each line. 
                        }
                        StringBuilder sb = new StringBuilder();
                        System.out.println(sb.toString());    
    
    
                } 
    
                System.out.println("Done!");
    
            }
    
            catch(IOException e)
            {
    
                e.printStackTrace();
    
            }
    
            finally
            {
    
                try
                {
    
                    if (br != null)
                    br.close();
    
                }
    
                catch(IOException ex)
                {
    
                    ex.printStackTrace();
    
                }
            }
    
        }
    

    注意:

    不要使用startsWith(...) 来检查第一个单词,因为它是按字符而不是按单词来检查的。如果您想检查单词 the,那么单词 there,their 也会返回 true,这可能会破坏您的代码。

    从现在开始尝试使用split() 而不是StringTokenizer

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 2021-12-08
      • 2019-02-10
      • 2020-06-09
      • 2019-02-25
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多