2016-10-29 87 views
0

我有一個程序使用用戶指定的文件名讀入文件。 必須讀取所有文件內容並將其存儲在數組中。除了這個錯誤之外,我似乎已經正確地完成了IO。我明白錯誤是什麼,但不知道如何糾正。字符串不能轉換爲數組

編輯:該數組已在文件中定義。

Zoo.java:284: error: incompatible types: String cannot be converted to Animals

animals[ j ] = bufferedReader.readLine(); 

這裏是我的READFILE子模塊代碼:

public String readFile(Animals[] animals)                  
{                            
    Scanner sc = new Scanner(System.in);                  
    String nameOfFile, stringLine;                   
    FileInputStream fileStream = null;                  
    BufferedReader bufferedReader;                   
    InputStreamReader reader;                     
    System.out.println("Please enter the filename to be read from.");           
    nameOfFile = sc.nextLine();                    
    try                          
    {                           
     constructed = true;                     
     fileStream = new FileInputStream(nameOfFile);               
     bufferedReader = new BufferedReader(new InputStreamReader(fileStream));        
     while((stringLine = bufferedReader.readLine()) != null)            
     {                          
      for(int j = 0; j < animals.length; j++)               
      {                         
       animals[j] = bufferedReader.readLine();              
      }                         
     }                          
     fileStream.close();                     
    }                           
    catch(IOException e)                      
    { 
     if(fileStream != null) 
     { 
      try 
      { 
       fileStream.close(); 
      } 
      catch(IOException ex2) 
      { 

      } 
     } 
     System.out.println("Error in file processing: " + e.getMessage(); 
    } 
} 

感謝您的幫助。

+0

動物[]數組在哪裏定義?你必須創建你的Animal類的新對象來填充數組。 '動物[j] =新動物(你的線);' –

+0

動物[]數組已經定義在同一個文件中。 – John

+0

使用將所有行讀取爲字符串,或使用字符串構建器。接下來使用字符串或stringbuilder的長度來創建你的數組。最後,在string/stringbuilder中使用for循環。長度和字符(i) – Sedrick

回答

1

animalsAnimals的數組,但是bufferedReader.readLine()讀出一行。您應該將其轉換爲Animal。我沒有看到你的類Animals的定義,但是,我認爲應該有一個構造函數,它需要String作爲參數。

所以,如果我是對的,你應該寫基本:

animals[j] = new Animals(bufferedReader.readLine());  
1

大量的代碼中的問題。從方法的輸入開始。也從文件中讀取。

public static void main(String[] args) { 
     // TODO code application logic here 
     for(String entry : readFile()) 
     { 
      System.out.println(entry); 
     } 
    } 

    static public String[] readFile()                  
    {                            
     Scanner sc = new Scanner(System.in);                 

     InputStreamReader reader;                     
     System.out.println("Please enter the filename to be read from.");           
     String nameOfFile = sc.nextLine();                   
     try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(nameOfFile)));)                          
     {                           
      //constructed = true; why?                  

      String stringLine; 

      ArrayList<String> arraylist = new ArrayList(); 
      while((stringLine = bufferedReader.readLine()) != null)            
      {                        
       arraylist.add(stringLine);              
      }  
      return arraylist.toArray(new String[0]); 
     } 
     catch (FileNotFoundException ex) 
     {                         
      Logger.getLogger(Filetoarray.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     catch (IOException ex) 
     { 
      Logger.getLogger(Filetoarray.class.getName()).log(Level.SEVERE, null, ex); 
     }                         
     return null; 
    }