-3

I'm trying to read in data from a csv file into a table from a database that I created. I've done it so far for 3 tables so the code for the fourth one should be relatively the same, but I keep receiving the error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 at Verbindung.main(Verbindung.java:136)

this should be a pretty simple problem to fix but I can't see where I'm going wrong as the index and number of variables in my array appear to match up fine.

Here is my code

  // Parsing CSV file Facebookapp 
           try {
                Scanner inputStream3 = new Scanner(file3);
                //inputStream.useDelimiter("[;/n]");
                while(inputStream3.hasNext()) {
                    String data3 = inputStream3.next();
                    System.out.println(data3);
                    String[] values3 = data3.split(";");    
                    System.out.println(data3);
                    //if(values.toString().contains(";;;")) break;
                    int PRODNUM = Integer.parseInt(values3[0]);
                    double V_ERSION = Double.parseDouble(values3[1]);
                    String PHONE_ASSOC = values3[2];
                    int accountNUM = Integer.parseInt(values3[3]);
                    //creating the object FBapp with the relevant parameters
                    FBapp fbapp = new FBapp(PRODNUM, V_ERSION, PHONE_ASSOC, accountNUM);
                    facebookapp.add(fbapp);
                    }
                inputStream3.close();
                }
                catch(FileNotFoundException d) {
                d.printStackTrace();
            }

    try {
        // establish connection to database
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String database = "jdbc:oracle:thin:@oracle-lab.cs.univie.ac.at:1521:lab";
        String user = "a01547605";
        String pass = "dbs19";
        Connection con = DriverManager.getConnection(database, user, pass);
        Statement stmt = con.createStatement();
        String sql3 = "INSERT into facebookapp(PRODNUM, V_ERSION, PHONE_ASSOC, accountNUM) values(?,?,?,?)";
        PreparedStatement ps = con.prepareStatement(sql3);

        final int batchSize = 100;
        int count = 0;
        for(FBapp d: facebookapp) {
            ps.setInt(1, d.getPRODNUM());
            ps.setDouble(2, d.getV_ERSION());
            ps.setString(3, d.getPHONE_ASSOC());
            ps.setInt(4, d.getaccountNUM());
            ps.addBatch();

            if(++ count% batchSize == 0) {
                ps.executeBatch();
            }
        }
          ps.close();

      }   

          catch (Exception e) {
          System.err.println(e.getMessage());
          }

My CSV file looks as follows, this is the first row of 100

1   1.00    Iphone  100

also when I print out what is being read out of my CSV file before it cuts out this is what it looks like

1;1.00;Iphone
Lion Heart
  • 1
  • 1
  • 4
  • The last line that you show ("1;1.00;Iphone") has only 3 values ("1", "1.00", "Iphone"), but you are trying to read to 4th value that does not exist on this line! – Thomas Kläger Jun 16 '19 at 12:32
  • it does in the CSV file, there is a fourth line "accountNUM" I clearly defined this in my code as well, I dont understand why it is not reading in the fourth column. Ive tried to see if there is something to correct in the csv file but everything looks totally fine – Lion Heart Jun 16 '19 at 12:35
  • Well, the first line that you show ("1 1.00 Iphone 100") has four columns, the last line ("1;1.00;Iphone") has only three columns. It seems that one of the lines in your CSV is missing a column. – Thomas Kläger Jun 16 '19 at 12:39
  • Ive opened it up with in my notepad just to see what it looks like there and here it is 1;1.00;Iphone ;100 looks totally normal... – Lion Heart Jun 16 '19 at 12:42
  • problem resolved, it didnt like the space after Iphone and ; – Lion Heart Jun 16 '19 at 12:53

1 Answers1

0

System.out.println(data3); is what's printing "1;1.00;Iphone" which should be the content of your CSV file. There's only three values on this line so since you hard-coded your program to expect a fourth value at int accountNUM = Integer.parseInt(values3[3]); it will fail here.

You also print data3 twice. Try printing out the contents of your array values3 in the second statement to catch code errors.

Ellime
  • 11
  • 1
  • 3