2017-09-23 15 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 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; 
    } 

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()機能です 私のコードは次の通りです最後に、私の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()に置き換える必要があります

関連する問題