我正在尝试解析这个csv文件。在由ItemId组成的第二列中,包含整数,但有些包含一个' X'在末尾。我试图删除该字符并输出一个新的csv文件。但是它似乎是我的条件陈述
if (itemId.charAt(itemId.length()-1) == 'X')
不满意。
CODE:
Scanner console = new Scanner(new File("data/BX-Book-Ratings.csv"));
PrintStream output = new PrintStream(new File("data/Book-Ratings.csv"));
String row;
String itemId;
while(console.hasNextLine())
{
row = console.nextLine();
Scanner inputRow = new Scanner(row).useDelimiter(";");
output.print(inputRow.next() + ","); //userid
itemId = inputRow.next();
if (itemId.charAt(itemId.length()-1) == 'X') {
itemId = itemId.substring(0, itemId.length() - 1);
}
long newitemId = Long.parseLong(itemId);
output.print(newitemId + ","); //itemid
output.println(inputRow.next()); //rating
}
DATA:
"276725";"034545104X";"0"
"276726";"0155061224";"5"
"276727";"0446520802";"0"
"276729";"052165615X";"3"
"276729";"0521795028";"6"
"276733";"2080674722";"0"
"276736";"3257224281";"8"
"276737";"0600570967";"6"
"276744";"038550120X";"7"
"276745";"342310538";"10"
"276746";"0425115801";"0"
"276746";"0449006522";"0"
答案 0 :(得分:2)
您的代码目前忽略了引号。但是你也需要处理它们:
itemId = inputRow.next();
if (itemId.charAt(itemId.length() - 2) == 'X') {
// Remember the end quote ------^
itemId = itemId.substring(1, itemId.length() - 2);
// Get rid of the quotes -^ and the X --------^
}
else {
itemId = itemId.substring(1, itemId.length() - 1);
// Get rid of the quotes -^--------------------^
}
long newitemId = Long.parseLong(itemId);