[英]Reading a CSV file with errors and printing the 'bad lines' to another file Java
[英]Reading and Printing a CSV File in JAVA
因此,分配是讀入一個CSV文件,其中包含諸如名稱,時間和原產國等項目,然后將它們打印到控制台。 我已經弄清楚了如何循環它,但是在第一行之后,對於接下來的大約一百個應該包含單個名稱時間等的輸出,我得到的都是null。這就是我所擁有的。
public class CSVReader {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
Scanner Scan = new Scanner(new File("./src/marathon.csv"));
Scanner timeScan = null;
int index = 0;
List<Racer> racerList = new ArrayList<>();
while(Scan.hasNextLine()){
timeScan = new Scanner(Scan.nextLine());
timeScan.useDelimiter(",");
Racer racer = new Racer();
while(timeScan.hasNext()){
String data = timeScan.next();
if(index==0)
racer.setFirstName(data);
else if(index==1)
racer.setLastName(data);
else if(index==2)
racer.setSexAge(data);
else if(index==3)
racer.setCountry(data);
else if(index==4)
racer.setPlace(data);
else if(index==5)
racer.setGunTime(data);
else if(index==6)
racer.setNetTime(data);
else if(index==7)
racer.setKm5(data);
else if(index==8)
racer.setKm10(data);
else if(index==9)
racer.setKm15(data);
else if(index==10)
racer.setKm20(data);
else if(index==11)
racer.setKm25(data);
else if(index==12)
racer.setKm30(data);
else if(index==13)
racer.setKm35(data);
else if(index==14)
racer.setKm40(data);
else if(index==15){
racer.setMinutesPerMile(data);
}
index++;
}
racerList.add(racer);
}
System.out.println(racerList);
}
}
您需要在每行之后將index
重置為0。
問題是您忘記了在while循環開始時將index
變量重置為0
。 在獲取String之前,只需在while循環內移動index
聲明即可:
while(timeScan.hasNext()){
int index = 0
String data = timeScan.next();
if(index==0)
//Rest of your code, if's, etc
並且不要忘記在掃描程序聲明之后從當前位置將其刪除:
Scanner Scan = new Scanner(new File("./src/marathon.csv"));
Scanner timeScan = null;
//int index = 0; //Remove this line from here
另外,您可能會考慮更改代碼,這是很多if語句的提示:在Racer
類中創建一個具有所有ifs並接受String
和int
參數的函數。
CSV文件的第一行有一個特殊行,其中包含以逗號分隔的字段名稱,
import java.io.IOException;
import java.util.ArrayList;
public class CSV {
public String[] header=null;
public String[][] table=null;
/**
* This method reads file lines into ArrayList,
* @param fileName the file to read from
* @return ArrayList of lines read
* @author Amr Lotfy
*
*/
public static ArrayList<String> load(String fileName){
ArrayList<String> lines=new ArrayList<String>();
if ((fileName!=null)&&(new File(fileName).exists())){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
try {
String line;
while ((line = br.readLine()) != null) {
// process the line.
//add to value list
lines.add(line);
}
} catch (IOException e) {
Logging.log(e.toString());
}
try {
br.close();
} catch (IOException e) {
Logging.log(e.toString());
}
} catch (FileNotFoundException e) {
Logging.log(e.toString());
}
}
return lines;
}
public CSV(String fileName) throws Exception{
ArrayList<String> lines=load(fileName);
if ((lines!=null)&&(lines.size()>0)){
header=lines.get(0).split(",");
table=new String[lines.size()-1][header.length];
for(int i=1; i<lines.size();i++){
String[] terms=lines.get(i).split(",");
for (int j=0;j<terms.length;j++){
table[i-1][j]=terms[j];
}
}
} else{
Logging.log("unable to load csv file.");
}
}
public String get(int rowIndex, String colHeader) throws Exception{
String result=null;
int colNumber=-1;
if (rowIndex>=0 && rowIndex<table.length){
for (int i=0; i<header.length;i++){
if (colHeader.equalsIgnoreCase(header[i])){
colNumber=i;
break;
}
}
if (colNumber!=-1){
result=table[rowIndex][colNumber];
}
}
return result;
}
}
樣本用法:
public void loadServerMap(String fileName) throws Exception{
String alias,url;
CSV csv=new CSV(fileName);
if (csv.table.length>0){
for (int i=0;i<csv.table.length;i++){
alias=csv.get(i, "alias");
url=csv.get(i, "url");
// You can print here but I will put the result in a map
serverMap.put(alias, url);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.