0

Following is the code I am using to read from a file and saving details from it in ArrayList of two classes Iphone and Ipad, but apparently something is going wrong with this.

The output I am getting for this code is:

Iphone@28d93b30 and IndexOutOfBoundException for ipads.get(0)

try {
            fScanner = new Scanner(new File("apple.txt"));
        } 
        catch (FileNotFoundException e) {
            System.out.println(e.toString());
        }

        ArrayList<Iphone> iphones = new ArrayList<>();
        ArrayList<Ipad> ipads = new ArrayList<>();

        while(fScanner.hasNextLine()) {
            String line = fScanner.nextLine();
            Scanner lineScanner = new Scanner(line);

            if(lineScanner.next().equals("IPHONE")) {

                    String model = "IPHONE"+lineScanner.useDelimiter(",").next();
                    String scrSze = lineScanner.useDelimiter(",").next();
                    String proc = lineScanner.useDelimiter(",").next();
                    String simT = lineScanner.useDelimiter(",").next();
                    String clr = lineScanner.useDelimiter(",").next();
                    String rom = lineScanner.useDelimiter(",").next();
                    String is3dtouch = lineScanner.useDelimiter(",").next();
                    String pric = lineScanner.useDelimiter(",").next();

                    iphones.add(new Iphone(model,scrSze,proc,simT,clr,rom,is3dtouch,pric)); 

            }


            else if(lineScanner.next().equals("IPAD")) {

                    String model = "IPAD"+lineScanner.useDelimiter(",").next();
                    String scrSze = lineScanner.useDelimiter(",").next();
                    String proc = lineScanner.useDelimiter(",").next();
                    String iswifi = lineScanner.useDelimiter(",").next();
                    String clr = lineScanner.useDelimiter(",").next();
                    String memo = lineScanner.useDelimiter(",").next();
                    String pric = lineScanner.useDelimiter(",").next();

                    ipads.add(new Ipad(model,scrSze,proc,iswifi,clr,memo,pric));

            }

        }

        System.out.println(iphones.get(2)+"\n");
        System.out.println(ipads.get(0)+"\n");

The file is like this:

IPHONE 7, 4.7, A10, GSM, JET BLACK, 32GB, TRUE, 700
IPAD AIR 2, 9.7, A8, TRUE, SILVER, 64GB, 400
IPHONE SE, 4, A9, CDMA, SILVER, 16GB, FALSE, 490
IPAD PRO, 9.7, A9, TRUE, SPACE GREY, 32GB, 650
IPHONE X, 7, A11, LTE, BLACK, 128GB, TRUE, 999
IPAD PRO X, 12, A12, TRUE, SPACE GREY, 256GB, 700

I'd appreciate a good explanation of the above problem.

  • You need to override the `toString()` method for it to print something useful. I suggest using your iDE to generate this method. – Peter Lawrey Oct 27 '18 at 11:43
  • `Scanner.next()` reads words between whitespace, not the text between `,` by default. I suggest you step through the code in your debugger to understand what it is doing. – Peter Lawrey Oct 27 '18 at 11:45

1 Answers1

2

You get Iphone@28d93b30 because you're printing an Iphone, and you haven't overridden the toString() method in the Iphone class, which means that the default Object.toString() method is being executed.

You get an exception because you're trying to get the first Ipad in the list, and there isn't any in the list. There isn't any because you're calling next() twice (once in the first if condition, and once in the second else if condition, so the token you're comparing with "IPAD" is the second one of the line, and not the first one.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255