2017-04-25 9 views
1

私は2つの文字列変数を持っている:Javaのファイル検索

givenAccntは」「accntTokenは」テキスト

の行のサブ(最初の文字列)であるユーザー

から取得した入力文字列であります

givenAccnt と等しい場合は、の行のを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... 
        } 
+0

デバッガを使用してコードを実行したときに気づいたことはありますか?マッチするたびに新しい 'StringBuilder'を作るのはなぜですか? 'StringBuilder'にマッチを追加するだけです。このコードは '+ fndPwrdsCNTR + PASSWORD(S)FOUND! 'をコンパイルしますか?"は構文エラーでなければなりません。 –

+0

こんにちはジョニー、はい、+ fndPwrdsCNTR + "パスワード(S)が見つかりました!"コンパイルする。私は愚かな間違いを見て、もう一度やり直すつもりです。ありがとうございました.. – codEinsteinn

答えて

0

各行に新しい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... 
        }