简体   繁体   中英

Read elements from a file and store them in ArrayList as Objects

I have a file contains (string, int, floating, char)

12

1.8

a

John

23.5

I want to read them an store them in an ArrayList. My ArrayList type is object. But, still I get exceptions when I run it.

    public static void main(String [] args) throws IOException{


    try{
            FileReader a=new FileReader("src/SET_A.txt");
            Scanner b=new Scanner(a);
            ArrayList<Object> c=new ArrayList<Object>();
            while(b.hasNextLine())
                {
                    Object d=b.nextInt();
                    c.add(d);
                 }
            System.out.println("The content of arraylist is: " + a);
    }
    catch(Exception exception)
    {
        exception.printStackTrace();
    }
    }

Are you displaying the File or the Contents of ArrayList? You should be displaying C and not A. If not, please let us know what is the Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM