【问题标题】:Java searching fileJava 搜索文件
【发布时间】:2017-04-25 03:44:50
【问题描述】:

我有 2 个字符串变量:

givenAccnt”是从用户获取的输入字符串

accntToken”是一行文本的子字符串(第一个字符串)

如果给定Accnt equals accntToken,我想返回 accntToken 匹配的文本。

此外,可能存在超过 1 个匹配项的情况。我想将所有匹配项保存到一个变量中,然后一次返回这些匹配项(行)。

下面的代码只在最后一行返回匹配。 (如果匹配在其他行它会错过它)

我似乎不明白为什么它会这样。

任何帮助将不胜感激。

givenAccnt = searchTextField.getText();//else, user is using search field to get given account
try
   {
      scanner = new Scanner(file);        //initialize scanner on file
      while(scanner.hasNextLine())        //while lines are being scanned
      {   
         getLine = scanner.nextLine();          //gets a line
         int i = getLine.indexOf(' ');          //get first string-aka-accnToken
         accntToken = getLine.substring(0, i);    
       }
       if(givenAccnt.equals(accntToken))   //if match   
       {
          collectedLines = new StringBuilder().append(getLine).toString();
          psswrdLabels = new JLabel(collectedLines, JLabel.LEFT);
          psswrdLabels.setAlignmentX(0);
          psswrdLabels.setAlignmentY(0);
          fndPwrdsCNTR += 1;     //counter for number of passwords found 
          JOptionPane.showMessageDialog(null, psswrdLabels ,+fndPwrdsCNTR+" PASSWORD(S) FOUND!", JOptionPane.INFORMATION_MESSAGE);    //shows window with matched passwords (as JLabels)
                            searchTextField.setText("");    //clears search field, if it was used
         }else
           //..nothing found
     }catch (FileNotFoundException ex) {
         //..problem processing file...
                    } 

【问题讨论】:

  • 当您使用调试器单步执行代码时,您注意到了什么?为什么每次找到匹配项时都创建一个新的StringBuilder?只需将匹配项附加到StringBuilder。此代码是否编译,+fndPwrdsCNTR+" PASSWORD(S) FOUND!" 应该是语法错误。
  • 您好 Jonny,是的,+fndPwrdsCNTR+" 找到密码!"编译。我看到了愚蠢的错误,打算再试一次。谢谢你..

标签: java file io save stringbuilder


【解决方案1】:

您不能在每一行上创建新的 StringBuilder。而是在阅读行之前创建它。代码:

givenAccnt = searchTextField.getText();//else, user is using search field to get given account
try
   {
builder=new StringBuilder();//initialize builder to store matched lines
      scanner = new Scanner(file);        //initialize scanner on file
      while(scanner.hasNextLine())        //while lines are being scanned
      {   
         getLine = scanner.nextLine();          //gets a line
         int i = getLine.indexOf(' ');          //get first string-aka-accnToken
         accntToken = getLine.substring(0, i);    
       }
       if(givenAccnt.equals(accntToken))   //if match   
       {
          collectedLines = builder.append(getLine).toString();
          psswrdLabels = new JLabel(collectedLines, JLabel.LEFT);
          psswrdLabels.setAlignmentX(0);
          psswrdLabels.setAlignmentY(0);
          fndPwrdsCNTR += 1;     //counter for number of passwords found 
          JOptionPane.showMessageDialog(null, psswrdLabels ,+fndPwrdsCNTR+" PASSWORD(S) FOUND!", JOptionPane.INFORMATION_MESSAGE);    //shows window with matched passwords (as JLabels)
                            searchTextField.setText("");    //clears search field, if it was used
         }else
           //..nothing found
     }catch (FileNotFoundException ex) {
         //..problem processing file...
                    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2011-12-27
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多