csv文件的Integer.parseInt在Java中不起作用
我试图将csv文件中的信息读入eclipse,然后将每一列中的信息读入相应的变量,但我一直遇到这个错误。我看到错误可能发生在代码的这一行 int accountNUM=Integer.parseInt(值[0]) 我得到的错误如下csv文件的Integer.parseInt在Java中不起作用,java,csv,Java,Csv,我试图将csv文件中的信息读入eclipse,然后将每一列中的信息读入相应的变量,但我一直遇到这个错误。我看到错误可能发生在代码的这一行 int accountNUM=Integer.parseInt(值[0]) 我得到的错误如下 Exception in thread "main" java.lang.NumberFormatException: For input string: "1;PhilJohnston;phil.johnston@gmail.com;" at java.ba
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)
当我将“,”更改为“;”时,我收到以下错误
线程“main”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;
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Scanner;
导入java.sql.*;
导入oracle.jdbc.driver.*;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
公共级逐字逐句{
公共静态void main(字符串[]args){
//导入表FUSER的CSV文件
字符串filename=“Fuser\u excel.csv”;
文件=新文件(文件名);
//创建Arraylist
ArrayListfuser=新的ArrayList();
//ArrayList lieferant=新的ArrayList();
//ArrayList lieferung=新的ArrayList();
试一试{
扫描仪输入流=新扫描仪(文件);
while(inputStream.hasNext()){
字符串数据=inputStream.next();
字符串[]值=data.split(“,”);
int accountNUM=Integer.parseInt(值[0]);
字符串N_AME=值[1];
//字符串EMAIL=值[2];
//System.out.println(accountNUM);
//使用相关参数创建对象FBuser
//FBuser FBuser=新FBuser(帐号、名称、电子邮件);
//fuser.add(fbuser);
}
inputStream.close();
}
捕获(FileNotFoundException d){
d、 printStackTrace();
}
试一试{
//建立与数据库的连接
类forName(“oracle.jdbc.driver.OracleDriver”);
String database=“jdbc:oracle:thin:@oracle-lab.cs.univie.ac.at:1521:lab”;
字符串user=“a01547605”;
字符串pass=“dbs19”;
//建立与数据库的连接
Connection con=DriverManager.getConnection(数据库、用户、通行证);
语句stmt=con.createStatement();
String sql=“插入fuser(accountNUM,N_name,EMAIL)值(?,?)”;
PreparedStatement ps=con.prepareStatement(sql);
最终int batchSize=1000;
整数计数=0;
用于(FBA用户:定影器){
ps.setInt(1,a.getaccountNUM());
ps.setString(2,a.getN_AME());
ps.setString(3,a.getEMAIL());
ps.addBatch();
如果(++计数%batchSize==0){
ps.executeBatch();
}
}
ps.close();
}
捕获(例外e){
System.err.println(e.getMessage());
}
}
}
对应的类
公共类用户{
int accountNUM;
字符串N_name;
字符串电子邮件;
公共FBuser(int accountNUM、字符串N_name、字符串电子邮件){
this.accountNUM=accountNUM;
this.N_AME=N_AME;
this.EMAIL=电子邮件;
}
public int getaccountNUM(){
返回accountNUM;
}
公共字符串getN_AME(){
返回N_name;
}
公共字符串getEMAIL(){
回复邮件;
}
}
我不明白我哪里出了问题。csv文件本身或读取方式可能有问题。根据您发布的csv文件,有两个问题。首先,分隔符不是逗号,而是空格。其次,如果将空格改为分隔符,则第一个值(值[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
您的CSV文件似乎被分割
不是通过,
(因此data.split(“,”
是错误的)CSV文件内容应该用逗号分隔,如下a、b、c、1、phil
我建议使用Commons CSV之类的库来解析输入文件。这为您处理了一些典型的问题,比如不同的分隔符、标题等。实际上这里没有标题,我只是想说明数据是在不同的列中分隔的。我还将分隔符从逗号改为空格,但仍然收到相同的错误。你是对的,我也意识到了这一点,但是如何去掉只有分号的行呢