英文:
I can not convert String to Integer (from csv file) -Error For input string: "4"
问题
private void getCSVData() {
inputStream = getResources().openRawResource(R.raw.book);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> type = new ArrayList<String>();
ArrayList<Integer> price = new ArrayList<Integer>();
ArrayList<Integer> bookId = new ArrayList<Integer>();
while ((csvLine = reader.readLine()) != null) {
String[] values = csvLine.split(",");
bookId.add(Integer.parseInt(values[0].trim()));
name.add(values[1]);
type.add(values[2]);
price.add(Integer.parseInt(values[3].trim()));
}
comparison(name, type, price, bookId);
} catch (IOException e) {
e.printStackTrace();
}
}
I use .getChars()
and get decimal code: 65279. It has no equivalent Ascii code. I realized that only the first character from all the data had this problem. Subsequent records have no problem.
[An image of debug console][1]
英文:
In Android studio - Java
I went different ways, but I didn't manage to turn String into Int. I have a csv file and I read its values. But I can't convert the id column to int.
private void getCSVData(){
inputStream = getResources().openRawResource(R.raw.book);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> type = new ArrayList<String>();
ArrayList<Integer> price = new ArrayList<Integer>();
ArrayList<Integer> bookId = new ArrayList<String>();
while ((csvLine = reader.readLine()) != null){
String[] values = csvLine.split(",");
bookId.add(Integer.parseInt(values[0].trim()));
name.add(values[1]);
type.add(values[2]);
price.add(Integer.parseInt(values[3].trim()));
}
comparison(name, type, price, bookId);
}
catch (IOException e) {
e.printStackTrace();
}
}
I use .getChars()
and get decimal code: 65279. It has no equivalent Ascii code. I realized that only the first character from all the data had this problem. Subsequent records have no problem
答案1
得分: 0
我认为输入字符串中包含双引号"
或其他类似的字符。
这可能是问题所在。
values[0].trim().replace(""\"", "\"\"")
===================编辑==========================
从问题描述中的日志看起来,这似乎不是普通的双引号,我检查了字符,似乎它们具有ASCII值8220和8221。所以你可以简单地这样做,
values[0].trim().replace("“\"", "\"\"");
values[0].trim().replace("”\"", "\"\"");
你还可以更通用地移除所有非数字字符,
StringBuilder builder = new StringBuilder();
for (int i = 0; i < values[0].length(); i++) {
char c = values[0].charAt(i);
if(c > 47 && c < 58){
builder.append(c);
}
}
这只是一个基本的解决方案,你也可以用流更优雅地实现,如果你愿意的话。
希望能有所帮助。
英文:
I think the input string has doublequotes "
or some other such character in it.
That must be the problem I guess.
values[0].trim().replace("\"", "")
===================EDIT==========================
From the log in the question description it seems like is not a normal double quote, I checked the chars and it seemed like it has ASCII values 8220 and 8221. So the easy way you can do is,
values[0].trim().replace("“", "");
values[0].trim().replace("”", "");
you can also do it more generically to remove all non numeric characters,
StringBuilder builder = new StringBuilder();
for (int i = 0; i < values[0].length(); i++) {
char c = values[0].charAt(i);
if(c > 47 && c < 58){
builder.append(c);
}
}
This is just a raw solution, you can do it more elegantly though with streams, I leave that part to you if you want to do it.
Hope it helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论