【问题标题】:How to read a specific word in text file and use a condition statement to do something如何读取文本文件中的特定单词并使用条件语句做某事
【发布时间】:2016-10-09 13:13:10
【问题描述】:

我是 Java 编码的新手。任何人都可以帮我处理我的代码吗?我目前正在制作一个程序,您在 jTextArea 中输入一个字符串,如果输入的单词与文本文件中的单词匹配,那么它将执行一些操作。

例如:我输入了“嘿”这个词,然后当输入的词与文本文件中的匹配时,它会打印“你好”之类的东西。

希望你明白我的意思。

这是我的代码:

String line;
    String yo;
    yo = jTextArea2.getText();

    try (
        InputStream fis = new FileInputStream("readme.txt");
        InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
        BufferedReader br = new BufferedReader(isr);
    ) 
    {
        while ((line = br.readLine()) != null) {

            if (yo.equalsIgnoreCase(line)) {
                System.out.print("Hello");
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(ArfArf.class.getName()).log(Level.SEVERE, null, ex);
    }

【问题讨论】:

    标签: java netbeans try-catch bufferedreader fileinputstream


    【解决方案1】:

    你不能对行使用等号,因为一行包含很多单词。您必须修改它以搜索一行中单词的索引。

           try (InputStream fis = new FileInputStream("readme.txt");
                    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
                    BufferedReader br = new BufferedReader(isr);) {
                while ((line = br.readLine()) != null) {
                    line = line.toLowerCase();
                    yo = yo.toLowerCase();
                    if (line.indexOf(yo) != -1) {
                        System.out.print("Hello");
                    }
                    line = br.readLine();
                }
            } catch (IOException ex) {
            }
    

    【讨论】:

    • 请从代码中删除 line = br.readLine(),因为该行是在 while((line = br.readLine)) 中分配的。如果你这样做,一些行将被跳过。
    【解决方案2】:

    由于您是 java 新手,我建议您花一些时间学习 java 8,它可以编写更简洁的代码。下面是用java 8写的解决方案,希望能给点帮助

            String yo = jTextArea2.getText();
            //read file into stream, 
            try (java.util.stream.Stream<String> stream = Files.lines(Paths.get("readme.txt"))) {
    
                List<String> matchLines = stream.filter((line) -> line.indexOf(yo) > -1).collect(Collectors.toList()); // find all the lines contain the text
                matchLines.forEach(System.out::println); // print out all the lines contain yo
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    【讨论】:

      【解决方案3】:
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.util.Scanner;
      
      public class WordFinder {
          public static void main(String[] args) throws FileNotFoundException {
              String yo = "some word";
              Scanner scanner = new Scanner(new File("input.txt")); // path to file
              while (scanner.hasNextLine()) {
                  if (scanner.nextLine().contains(yo)) { // check if line has your finding word
                      System.out.println("Hello");
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多