0

I have a program that reads in a file using a filename specified by the user. All file contents must be read and stored in the array. I seem to have done the IO Correctly besides this error. I understand what the error is but not sure how to correct.

EDIT: The array is already defined in the file.

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

animals[ j ] = bufferedReader.readLine();

Here is my code for the readFile Submodule:

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

Thanks for the help.

7
  • Where is animals[ ] array defined? you have to create new object of your Animal class to fill in the array. animal [j] = new Animal( your line); Commented Oct 29, 2016 at 3:40
  • Animals [] array is already defined in the same file. Commented Oct 29, 2016 at 3:41
  • Use read all lines as a string, or use a string builder. Next create your array using the length of the string or stringbuilder. Lastly, use a for loop with string/stringbuilder. Length and chaAt (i) Commented Oct 29, 2016 at 3:48
  • If by line, first create an arraylist and use arraylist.add (). Then do String[] array = arraylist.toArray (); Commented Oct 29, 2016 at 3:52
  • I am using my phone so I can't post an answer. Commented Oct 29, 2016 at 3:53

2 Answers 2

1

animals is array of Animals, but bufferedReader.readLine() reads line. You should convert it to Animal. I don't see definition of your class Animals, but, I think, there should be constructor that takes String as argument.

So, If i'm right, you should basically write:

animals[j] = new Animals(bufferedReader.readLine());     
Sign up to request clarification or add additional context in comments.

Comments

1

Lots of problems in your code. Starting with the method's input. Also reading from file.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.