CSV文件的Integer.parseInt在Java中不起作用

时间:2019-06-15 13:48:07

标签: java csv

我试图将csv文件中的信息读入eclipse,然后将每一列读入各自的变量,但是我一直收到此错误。我看到错误可能是在代码的这一行

  

int accountNUM = Integer.parseInt(values [0]);

我得到的错误如下

Exception in thread "main" java.lang.NumberFormatException: For input string: "1;PhilJohnston;phil.johnston@gmail.com;"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at Verbindung.main(Verbindung.java:55)

当我将“,”更改为“;”时我收到以下错误

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:索引0超出长度0     在Verbindung.main(Verbindung.java:55)

我的csv文件是这种格式

A            b               c
1        PhilJohnston     phil.johnston@gmail.com 

相应的Java代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.sql. * ;
import oracle.jdbc.driver. * ;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Verbindung {

    public static void main(String[] args) {

        //Importing CSV File for table FUSER
        String filename = "Fuser_excel.csv";
        File file = new File(filename);

        // Creating Arraylist
        ArrayList < FBuser > fuser = new ArrayList < FBuser > ();
        //  ArrayList<Suppliers> lieferant = new ArrayList<Suppliers>();
        //ArrayList<Delivery> lieferung = new ArrayList<Delivery>();

        try {
            Scanner inputStream = new Scanner(file);
            while (inputStream.hasNext()) {
                String data = inputStream.next();
                String[] values = data.split(",");
                int accountNUM = Integer.parseInt(values[0]);
                String N_AME = values[1];
                //String EMAIL = values[2];
                //System.out.println(accountNUM);
                //creating the object FBuser with the relevant parameters
                //FBuser fbuser = new FBuser(accountNUM,N_AME,EMAIL);
                //fuser.add(fbuser);
            }
            inputStream.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";

            // establish connection to database 
            Connection con = DriverManager.getConnection(database, user, pass);
            Statement stmt = con.createStatement();

            String sql = "INSERT into fuser (accountNUM, N_AME, EMAIL) values(?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);

            final int batchSize = 1000;
            int count = 0;
            for (FBuser a: fuser) {
                ps.setInt(1, a.getaccountNUM());
                ps.setString(2, a.getN_AME());
                ps.setString(3, a.getEMAIL());
                ps.addBatch();

                if (++count % batchSize == 0) {
                    ps.executeBatch();
                }
            }
            ps.close();
        }
        catch(Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

the corresponding Class
public class FBuser {
    int accountNUM;  
    String N_AME;
    String EMAIL;

    public FBuser(int accountNUM,String N_AME, String EMAIL ) {
        this.accountNUM = accountNUM;
        this.N_AME = N_AME;
        this.EMAIL = EMAIL;
    }

    public int getaccountNUM() {
        return accountNUM;
    }

    public String getN_AME() {
        return N_AME;
    }

    public String getEMAIL() {
        return EMAIL;
}
}

我不明白我在哪里出问题了。 csv文件本身或读取文件的方式可能有问题。

2 个答案:

答案 0 :(得分:1)

根据您发布为CSV文件的内容,有两个问题。首先,分隔符不是逗号,而是空格。其次,如果更改为空格作为分隔符,则第一个值(value [0])将为'A'。 Java无法将“ A”转换为数字。

答案 1 :(得分:0)

如果字符串仅包含您作为方法参数提供的定界符,则split()返回的数组的长度只能为0。

例如,";".split(";").length返回0。

ArrayIndexOutOfBoundsException是由仅包含;的行引起的。

如果CSV文件中的一行仅包含定界符,则表示所有单元格均为空。如果要摆脱这些行,则应在CSV文件中将其删除。如果要跳过这些行,则只需检查结果数组是否为空:

String[] cells = line.split(";");
if (cells.length > 0) {
    // Parse lines
}
// Else do nothing