2016-10-29 7 views
0

사용자가 지정한 파일 이름을 사용하여 파일을 읽는 프로그램이 있습니다. 모든 파일 내용을 읽고 배열에 저장해야합니다. 나는이 오류 외에도 IO를 올바르게 수행 한 것 같습니다. 오류가 무엇인지 이해하지만 해결 방법을 모르겠습니다.문자열을 배열로 변환 할 수 없습니다.

EDIT : 배열이 이미 파일에 정의되어 있습니다. 도움을

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(); 
    } 
} 

감사 :

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

animals[ j ] = bufferedReader.readLine(); 

다음은 ReadFile을 서브 모듈 내 코드입니다.

+0

animals [] 배열은 어디에 정의되어 있습니까? 배열을 채우기 위해 Animal 클래스의 새 객체를 만들어야합니다. 'animal [j] = new Animal (your line); –

+0

Animals [] 배열은 이미 같은 파일에 정의되어 있습니다. – 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; 
    } 
관련 문제