[英]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.