繁体   English   中英

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

[英]Integer.parseInt of csv file not working in Java

我试图将信息从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:在Verbindung.main(Verbindung.java:55)处,索引0的长度为0超出范围

我的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档案本身或读取方式可能有问题。

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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