【问题标题】:find a word in txt and show the line在txt中找到一个单词并显示该行
【发布时间】:2015-06-14 08:26:57
【问题描述】:

我坚持做这个练习,我必须匹配用户的输入和 txt 文件中的单词,并且匹配必须显示整行,但我不知道该怎么做,我有代码但我不确定它是否正确

//codes01.dat

IXE, Mangalore, India

BOM, Mumbai, India

PPP, Proserpine Queensland, Australia

RKT, Ras Al Khaimah, United Arab Emirates

输入:ProseRPine

输出:PPP,澳大利亚昆士兰 Proserpine


   import java.io.*;
import java.util.*;
public class pruebas
{
    public static void main(String[] args) throws java.io.IOException
    {
        System.out.print(" enter airport name");
        Scanner keyboard = new Scanner(System.in);
        String input = keyboard.nextLine();
        BufferedReader br = new BufferedReader(new FileReader("codes01.dat"));
        String s1;
        String s2;
        String strline;
        while (strline = br.readLine())
        {
            s1 = br.readLine();
            int numTokens = 0;
            StringTokenizer st = new StringTokenizer(s1);

            while (st.hasMoreTokens()) {
                s2 = st.nextToken();
                ++numTokens;

                if (s2.equalsIgnoreCase(input)) {
                    System.out.pritn(s1);
                } else {
                    System.out.print(" no data found ");
                }
            }
        }
    }
}

【问题讨论】:

  • 请格式化代码
  • 解决编译器错误。发布的代码不会编译。
  • 两个 br.readLine()。不会是正确的。
  • 不需要指定分隔符吗?
  • 您的 while 循环应该比较一个布尔值,但它正在为字符串分配一个布尔值。有两个错误(除了其他可能的逻辑错误)1.)您需要在 while 中提供一个布尔值,例如 while (br.readLine()) 2.) System.out.pritn(s1) 应该是 System.out.println(s1)

标签: java string class if-statement while-loop


【解决方案1】:

你使用Java-8,你可以优雅地使用BufferedReader#lines解决这个问题。

BufferedReader br= new BufferedReader(new FileReader(new File("codes01.dat")));
String input= "ProseRPine";
String output= br.lines()
  .filter(e-> e.toLowerCase().contains(input.toLowerCase())).findFirst().get();

【讨论】:

    【解决方案2】:

    您有 4 个错误:

    1) while 循环条件应返回 boolean

    2) FileNotFoundException 未被捕获。使用 try catch 块

    3) IOException 未被捕获。使用 try catch 块

    4) pritn() 方法应改为print();而不是print(),你可以使用println()

    你的核心逻辑也很好,你只需要调试一下。如果你愿意,你可以使用String#contains() 方法。

    【讨论】:

      【解决方案3】:
       /* package whatever; // don't place package name! */
      
      import java.io.*;
      
      import java.util.*;
      
      class Ideone
      {
      public static void main (String[] args) throws java.io.IOException
      {
      String s1;
      String s2="";
      String input="Proserpine";
      String strline;
         s1 = "PPP, Proserpine Queensland, Australia";
         int numTokens = 0;
         StringTokenizer st = new StringTokenizer(s1);
      
        while (st.hasMoreTokens())
        {
           s2 = st.nextToken();
           ++numTokens;
            if (s2.equalsIgnoreCase(input))
            {
            System.out.print(s1);
           break;
           }
      
        }
       } 
       }
      

      成功时间:0.1 内存:321344 信号:0PPP,Proserpine 昆士兰,澳大利亚

      【讨论】:

        【解决方案4】:

        我还没有尝试过你的代码,但是在理解了需求后,我可以向你推荐这个例子.. http://stackoverflow.com/questions/14675452/find-whole-line-that-contains-word-with-php-regular-expressions

        使用以下正则表达式模式pattern = '/^.*\bsession\b.*$/m'; 匹配您的字符串并查看是否检索到整行

        【讨论】:

          【解决方案5】:

          从文件中读取每一行,保存在字符串中并检查是否public boolean contains(CharSequence s)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-07-07
            • 2023-03-15
            • 1970-01-01
            • 1970-01-01
            • 2022-11-15
            • 2021-03-29
            相关资源
            最近更新 更多