There is nothing wrong in the other part. I read the lines of the excel files. But I want to split the cells of this excel. I use the statement String []values1 = data1.split(",");
and values1[5]=values1[5].replace('"', ' ').trim();
. But there is a particular cell which there is a ,
in it. This comma disrupts the arranged order. How should I deal with it?
File DopCan = new File(filesnames[0]);
try
{
Scanner scanner = new Scanner(DopCan);
scanner.nextLine();
scanner.nextLine();
while(scanner.hasNext())
{
String data = scanner.nextLine();
String []values = data.split(",");
values[5]=values[5].replace('"', ' ').trim();
if(Integer.parseInt(values[5])==0)
{
i++;
}
else
{
break;
}
}
scanner.close();
Scanner scanner1 = new Scanner(DopCan);
scanner1.nextLine();
scanner1.nextLine();
while(scanner.hasNext())
{
ArrayList <PollingPlace> files = new ArrayList();
while (scanner1.hasNext())
{
int n=0;
String data1 = scanner1.nextLine();
String []values1 = data1.split(","); //Problem here
scanner1.nextLine();
String data3=scanner1.nextLine();
String []values3 = data3.split(",");
values1[5]=values1[5].replace('"', ' ').trim();
values1[7]=values1[7].replace('"', ' ').trim();
values1[14]=values1[14].replace('"', ' ').trim();
values3[14]=values3[14].replace('"', ' ').trim();
files.add(new PollingPlace(Integer.parseInt(values1[5]),Integer.parseInt(values1[7]),Integer.parseInt(values1[14]),Integer.parseInt(values3[14])));
scanner1.nextLine();
scanner1.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
There's an easy answer and a correct one.
Easy answer is to parse the string more robust with regexp, say,
(?:\s*(?:\"([^\"]*)\"|([^,]+))\s*,?)+?
But CSV turns out to be a rather diffucult for that as there are a number of corner cases to consider.
Correct answer is to use a library for that, and as it happens there is manifold of them: JSefa , OpenCSV , etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.