2016-12-06 3 views
0

[SOLVED]その他ファイルからの読み取り中に常に実行する場合

ステートメントに達したかどうかのマーカーを追加することで修正しました。ファイルからの例SSNを読み込む必要プログラムを書く

if(contains) // condition reached. marker "found" is 1. 
    found = 1; 
else if(found != 1){  // If found is not 1, not found. break. 
System.out.println("Not found"); break; 

。私は無効な入力を考慮しようとしています(つまり、文字列がファイル内にない)。しかし、このステートメントは、私がそれらをどのようにしたいのかを実行しません。 (if文がどのように構造化されているかを無視します)。

public static void getNameNum(String SocSec_input){ 
    try{ 
     reader1 = new BufferedReader(new FileReader("src\\DEPARTMENT.txt")); 
     reader2 = new BufferedReader(new FileReader("src\\EMPLOYEE.txt")); 

     /** 
     * reads from employee text file 
     */ 
     while((curr = reader2.readLine()) != null){ 
      /** 
      * checks which lines contain user input and split the name from the code into storage 
      */ 

      if(curr.contains(SocSec_input)){ 
       String[] parts = curr.split(","); 
       String FNAME = parts[0]; 
       String LNAME = parts[1]; 
       String SSN = parts[2]; 
       String DNO = parts[9]; 

       if(SSN.equals(SocSec_input)){ 
        while((curr = reader1.readLine()) != null){ 
         /** 
         * searches the file for the user input 
         */ 
         if(curr.contains(DNO)){ 
          /** 
          * splits the line containing user input at each comma and store the values 
          */ 
          String[] parts2 = curr.split(","); 
          String DNAME = parts2[0]; 
          String DNUMBER = parts2[1]; 

          if(DNUMBER.equals(DNO)) 
           System.out.println(FNAME + " " + LNAME + " works in department " + DNO + ", " + DNAME); 
         } 
        } 
       } 
      } 
      // ALWAYS EXECUTING STATEMENT HERE***************** 
      else if(!(curr.contains(SocSec_input))){ 
       System.out.println("Invalid SSN entered. We could not find 
            that SSN in our database."); 
       break; 
      } 
     } 
    } 
    catch (IOException e){ 
     e.printStackTrace(); 
    } 
} 

出力:

Please enter the employee's SSN: 123 
Invalid SSN entered. We could not find that SSN in our database. 

Please enter the employee's SSN: 888665555 
Invalid SSN entered. We could not find that SSN in our database. 
-------------------- 

しかし888665555がファイル内にあります!何が起こっている?

+0

に確認してみてください、あなたの文字列を使用すると、彼らが何を考えていることを確認するためにCURRに書式設定文字列を使用してみたのですか? – Tosh

+0

@Toshお返事ありがとうございます。文字列のフォーマットはどういう意味ですか? – pmcg521

+1

は、基本的には素早くデバッグを行います。いくつかのprintステートメントと同じくらい簡単に作業していると思う文字列が、コンピュータが渡している文字列と同じであるかどうかを確認し、そうでない場合は文字列.formatを使用して文字列を見た目に見せる – Tosh

答えて

-4

戻り値の型はbooleanです。 true/false

if(curr.contains(SocSec_input) == true){ 
} 

else if(curr.contains(SocSec_input) == false){ 
} 
+0

誰かが間違ったことをコメントできますか? – roottraveller

関連する問題