【问题标题】:String cannot be converted to array字符串不能转换为数组
【发布时间】:2017-03-11 23:27:15
【问题描述】:

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

EDIT:该数组已在文件中定义。

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

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

感谢您的帮助。

【问题讨论】:

  • 动物[]数组定义在哪里?您必须创建 Animal 类的新对象来填充数组。 animal [j] = new Animal( your line);
  • Animals [] 数组已在同一个文件中定义。
  • 使用读取所有行作为字符串,或使用字符串生成器。接下来使用字符串或字符串生成器的长度创建数组。最后,使用带有 string/stringbuilder 的 for 循环。长度和chaAt (i)
  • 如果按行,先创建一个arraylist,使用arraylist.add()。然后做 String[] array = arraylist.toArray();
  • 我正在使用手机,所以无法发布答案。

标签: java arrays io incomplete-type


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您的代码中有很多问题。从方法的输入开始。也从文件中读取。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      相关资源
      最近更新 更多