2017-09-23 17 views
-3

파일에서 문자열을 입력하고 각 문자열이 주어진 문자열과 일치하는지 확인하고 싶지만 작성할 수 없습니다. 도움을 주셔서 미리 감사드립니다. 내 코드는 다음과 같습니다 :파일에서 문자열을 입력하고 입력 문자열을 비교하십시오.

import java.util.*; 
import java.io.*; 
import org.apache.commons.io.*; 
import java.nio.charset.*; 

public class XYZ 
{ 
    String s[] = {"Harry","Potter","Pirates","Of","The","Carribean"}; 

    XYZ() 
    { 
     save(); 
     String []copyString = load(); 
     for(int i=0; i<6; i++) 
     { 
      System.out.print("String " + i + ": " + copyString[i]); 
     } 
    } 

이 파일에 문자열을 저장하는 내 save() 기능입니다 :

public void save() 
{ 
    DataOutputStream output = null; 
    try 
    { 
     output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("the-file-name.txt"))); 
     for(int i=0; i<6; i++) 
     { 
      output.writeUTF(s[i]); 
     } 
     output.close(); 
    } 
    catch(IOException e){}  

} 

이 문자열 반환 내 load() 기능입니다 :

public String[] load() 
    { 
     final Charset UTF_8 = Charset.forName("UTF-8");  
     String temp; 
     String copyFromFile[] = new String[6]; 

     DataInputStream input = null; 
     try 
     { 
      input = new DataInputStream(new BufferedInputStream(new FileInputStream("the-file-name.txt"))); 
      for(int i=0; i<6; i++) 
      { 

       temp = input.readUTF(); 
       if(temp == "Harry" || temp == "Potter" || temp == "Pirates" || temp == "Of" || temp == "The" || temp == "Carribean") 
       { 
        copyFromFile[i] = temp; 
       } 
       else 
       { 
        System.out.println("Not matched"); 
       } 
      } 
      input.close(); 
     }catch (IOException e){} 
     return copyFromFile; 
    } 

을 마지막으로 내 main() 기능 :

public static void main(String[]arg) 
{ 
    XYZ xyz = new XYZ(); 

} 

}

출력 : 문자열 비교에 대한

Not matched 
Not matched 
Not matched 
Not matched 
Not matched 
Not matched 

답변

0

사용 .equals. temp == "Harry"...를 temp.equals()로 바꿔야합니다.