2016-10-29 26 views
0

ユーザーが指定したファイル名でファイルを読み込むプログラムがあります。 すべてのファイルの内容を読み取り、配列に格納する必要があります。このエラーのほかにIOを正しく行ったようです。エラーの内容を理解していますが、修正する方法がわかりません。文字列を配列に変換できません

EDIT:配列はすでにファイルに定義されています。ここで

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クラスの新しいオブジェクトを作成する必要があります。 'animal [j] =新しい動物(あなたの行); –

+0

動物[]配列はすでに同じファイルに定義されています。 – John

+0

すべての行を文字列として読み込むか、文字列ビルダーを使用します。次に、文字列またはstringbuilderの長さを使用して配列を作成します。最後に、forループをstring/stringbuilderとともに使用します。長さとchaAt(i) – Sedrick

答えて

1

animalsは、Animalsの配列ですが、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; 
    } 
関連する問題