无法将字符串转换为整数(来自CSV文件)- 输入字符串错误:“4”

huangapple go评论72阅读模式
英文:

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&lt;String&gt; name = new ArrayList&lt;String&gt;();
            ArrayList&lt;String&gt; type = new ArrayList&lt;String&gt;();
            ArrayList&lt;Integer&gt; price = new ArrayList&lt;Integer&gt;();
            ArrayList&lt;Integer&gt; bookId = new ArrayList&lt;String&gt;();
            while ((csvLine = reader.readLine()) != null){
                String[] values = csvLine.split(&quot;,&quot;);
                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

得分: 0

我认为输入字符串中包含双引号&quot;或其他类似的字符。
这可能是问题所在。

values[0].trim().replace("&quot;\"", "\"\"")

===================编辑==========================

从问题描述中的日志看起来,这似乎不是普通的双引号,我检查了字符,似乎它们具有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 &quot; or some other such character in it.
That must be the problem I guess.

values[0].trim().replace(&quot;\&quot;&quot;, &quot;&quot;)

===================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(&quot;“&quot;, &quot;&quot;);
values[0].trim().replace(&quot;”&quot;, &quot;&quot;);

you can also do it more generically to remove all non numeric characters,

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i &lt; values[0].length(); i++) {
            char c = values[0].charAt(i);
            if(c &gt; 47 &amp;&amp; c &lt; 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

huangapple
  • 本文由 发表于 2020年4月10日 13:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定