2016-09-16 17 views
1

私のプロジェクトに関連したコーディングをしています。ファイルから読み込む文字列配列に単に文字列値を代入する必要があります。文字列の値を文字列配列に割り当てる

しかし、値が常にnullのままになる理由はわかりません。 stringの値は配列に代入されません。誰かが私にした間違いを説明することができますか?

ここにコードを掲載しました。

Test.java

public class Test { 

    public static void main(String[] args) throws IOException { 
     //Tuning Jaccard Coefficient algorithm for Training set 
     ReadFile_2 rf = new ReadFile_2();   
     rf.readFile("C:/Users/user/Desktop/Msc-2016/InformationRetrieval/project material/train.txt","Training"); 
    } 
} 

ReadFile_2.java

class ReadFile_2 { 

    List<String> copying_strings1 = new ArrayList<>(); 
    String[] Apparted_Strings = new String[3]; 
    String[] copying_strings = new String[50]; 
    int arryListSize = copying_strings.length; 
    static int value_of_shingle; 
    static int best_Shingle; 
    String[] fileType; 
    int fileType_size; 

    public void readFile(String fileName, String file_type) throws FileNotFoundException, IOException { 

     //Name of the file 
     try { 
      if (file_type.equals("Training")) { 
       best_Shingle = 2; 
      } else if (file_type.equals("Testing")) { 
       best_Shingle = value_of_shingle; 
      } 

      FileReader inputFile = new FileReader(fileName); 
      BufferedReader bufferReader = new BufferedReader(inputFile); 
      String line; 
      int r = 0; 

      while ((line = bufferReader.readLine()) != null) { 
       copying_strings[r] = line; 
       r++; 
       System.out.println("lll " + copying_strings[r]); 
       System.out.println("lll " +line); 
       //Apparted_Strings = sp.apart_Strings_3(line); 
       //CallingAlgo_4 c_a = new CallingAlgo_4(Apparted_Strings[0], Apparted_Strings[1], Apparted_Strings[2], best_Shingle, "Jaccard"); 
      } 

      //Close the buffer reader 
      bufferReader.close(); 
     } catch (Exception e) { 
      System.out.println("Error while reading file line by line:" + e.getMessage()); 
     } 
    } 
} 

誰かが私がnullとして、なぜ

System.out.println("lll " + copying_strings[r]); 

プリントの値は常に知らせてくださいすることができます。

+2

while ((line = bufferReader.readLine()) != null) { copying_strings[r] = line; System.out.println("lll " + copying_strings[r++]); System.out.println("lll " +line); } 
'R ++の変数(r)をインクリメントしてください;'。読み込みと印刷の間に 'r'をインクリメントします。その結果、次の行に何を印刷するのですか?まだ割り当てていないので、いつも' null'があります。 'r ++'を削除して印刷を 'copying_strings [r ++]'に変更し、 'r'をインクリメントしながらカレント行を出力してください。 – SomeJavaGuy

答えて

0

while-loopに間違いがあります。正しい順序は、最初に行を読み、それをStringに渡して出力し、最後にループ変数をインクリメントすることです。

while ((line = bufferReader.readLine()) != null) {  // read a line 
    copying_strings[r] = line;       // pass to String 
    System.out.println("lll " + copying_strings[r]); // print for the 1st time 
    System.out.println("lll " + line);     // print for the 2nd time 
    r++;            // increment the looped variable 
} 

あなたがr++をインクリメントした後、変数copying_stringsを印刷する場合は何もそれに渡されていないので、あなたは明らかにnull得ます。

0
  • 文字列値を出力する前にwhileループ変数(r)をインクリメントします。
  • したがって、nullという次の配列値が出力されます。

したがって、後述するように文字列の値を印刷した後

関連する問題