2017-04-25 6 views
1

찾고 : 자바는 I 2 개 문자열 변수가 파일

은 " givenAccnt는"

사용자

" accntToken"에서 얻어진 입력 문자열을 텍스트

의 라인의 문자열 (제 1 캐릭터) 인

givenAccnt 과 같으면 줄의이 accntToken과 일치해야합니다.

또한 일치하는 항목이 두 개 이상인 경우가있을 수 있습니다. 모든 일치 항목을 변수에 저장 한 다음 해당 일치 항목 (줄)을 즉시 반환하려고합니다.

아래 코드는 마지막 줄의 일치 항목 만 반환합니다. (일치하는 항목이 다른 줄에 있으면 놓치기 만합니다.)

왜 그런 식으로 행동하는지 파악할 수 없습니다.

도움을 주시면 감사하겠습니다.

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... 
        }