2017-04-03 110 views
0

我得到一个错误与错误与文件阅读器

PersonsInfoData[i] = PersonsInfoin.setPersonsInfo(temp[0],temp[1],temp[2],temp[3],temp[4]); 

“不兼容类型:无效不能coverted到PersonsInfo”

public void readFile(String fileName) 
{ 
    // Try to read in the data and if an exception occurs go to the Catch section 
    try 
    { 
     FileInputStream fstream = new FileInputStream(fileName); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

     int i = 0; // i is used as the line counter 
     String line; // line is used to temporarily store the line read in from the data file 

     // Read a line from the data file into the buffer and then check whether 
     //  it is null. The while loop continues until a line read in is null. 
     while ((line = br.readLine()) != null) 
     { 
      // Split the line of data (from the text file) and put each entry into the 
    //            temporary array - temp[] 
      String[] temp = line.split(","); 
      // Save each entry into its respective PCDataRecord object. 
      PersonsInfoData[i] = PersonsInfoin.setPersonsInfo(temp[0],temp[1],temp[2],temp[3],temp[4]); 
      i++; // Increment i so we can keep a count of how many entries have been read in. 
     } 
     numberOfEntries = i; // Set numberOfEntries equal to i, to remember how many entries are now in the array 
     br.close();   // Close the BufferedReader 
     in.close();   // Close the DataInputStream 
     fstream.close();  // Close the FileInputStream 
    } 
    catch (Exception e) 
    { 
     System.err.println("Error Reading File: " + e.getMessage()); 
    } 
} 

回答

1

这是一个二传手声明:

PersonsInfoin.setPersonsInfo(temp[0],temp[1],temp[2],temp[3],temp[4]); 

因此,并非所有的指定者返回对象,你的情况是方法返回返回任何(void)使您的发言相当于做:

PersonsInfoData[i] = void 

这是无效的......

+1

我认为你是正确的这个例子中,基于该错误消息,但:(1)一个“二传手”就像任何其他的方法,所以称它为“二传表”并非真正正确; (2)并非所有的setter都返回void,事实上,setter返回'this'对象以使它们可以链接是相当普遍的。 – ajb

+0

@ajb我将编辑addint您的评论... thnks! –

0

我想你正试图将值写入PersonsInfoData [一世]。如果是,则与替换线 -

PersonsInfoData[i].setPersonsInfo(temp[0],temp[1],temp[2],temp[3],temp[4]);