字符串无法转换为数组

时间:2016-10-29 03:32:57

标签: java arrays io incomplete-type

我有一个程序,它使用用户指定的文件名读入文件。 必须读取所有文件内容并将其存储在数组中。除了这个错误,我似乎已经正确完成了IO。我理解错误是什么,但不确定如何纠正。

编辑:数组已在文件中定义。

  

Zoo.java:284:错误:不兼容的类型:字符串无法转换为   动物

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

这是我的readFile子模块的代码:

any( seq[i-1] == seq[i] == 2 for i in range(1,len(seq)) )

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

animalsAnimals的数组,但bufferedReader.readLine()读取行。您应该将其转换为Animal。我没有看到你的类Animals的定义,但是,我认为应该有一个以String为参数的构造函数。

所以,如果我是对的,你基本上应该写:

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

答案 1 :(得分: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;
    }
相关问题