简体   繁体   English

NumberFormatException:对于输入字符串:“2”

[英]NumberFormatException: For input string: “2”

I have a list of strings. 我有一个字符串列表。 First element is: 第一个要素是:

2       helloworld                    10173.991234

I've written the code below: 我写了下面的代码:

            ArrayList<Integer> idList = new ArrayList<Integer>();
            for (String s:list){
                String subs = s.substring(0,8);
                subs = subs.trim();
                idList.add(Integer.valueOf(subs));
            }

This code shoud parse first id field and add it to arraylist. 这段代码shoud解析第一个id字段并将其添加到arraylist。 But it fails on line idList.add(Integer.valueOf(subs)); 但它在线上失败了idList.add(Integer.valueOf(subs));

Whats the problem? 有什么问题? Any help? 有帮助吗?

Upd: UPD:

public class Solution {
    public static void main(String[] args) throws Exception {
        if (args[0].equals("-c")) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String fileString = reader.readLine();
            reader.close();
            Scanner scanner = new Scanner(new File(fileString));
            ArrayList<String> list = new ArrayList<String>();
            while (scanner.hasNextLine()) {
                list.add(scanner.nextLine());
            }
            String ne = list.get(list.size()-1);
            scanner.close();
            int maxId;
            if (ne.length()>1) {
                ArrayList<Integer> idList = new ArrayList<Integer>();
                for (String s:list){
                    String subs = s.substring(0,8);
                    subs = subs.trim();
                    idList.add(Integer.parseInt(subs));
                }
                maxId = idList.get(0);
                for (int i:idList){
                    if (maxId<i){
                        maxId=i;
                    }
                }
                maxId++;
            }
            else {
                maxId = 0;
            }
            String maxIdString = ""+maxId;
            while (maxIdString.length()<8){
                maxIdString+=" ";
            }
            if (maxIdString.length()>8){
                maxIdString = maxIdString.substring(0,8);
            }
            String productName = "";
            for (int i = 1; i < args.length-2; i++) {
                productName+=args[i]+" ";
            }
            productName = productName.trim();
            while (productName.length()<30){
                productName+=" ";
            }
            if (productName.length()>30)
                productName=productName.substring(0,30);
            String price = args[args.length-2];
            while (price.length()<8){
                price+=" ";
            }
            if (price.length()>8)
                price=price.substring(0,8);
            String quantity = args[args.length-1];
            while (quantity.length()<4){
                quantity+=" ";
            }
            if (quantity.length()>4)
                quantity=quantity.substring(0,4);
            String outString = maxIdString+productName+price+quantity;
            FileOutputStream outputStream = new FileOutputStream(fileString,true);
            if (ne.length()>1)
                outputStream.write("\r\n".getBytes());
            outputStream.write(outString.getBytes());
            outputStream.close();
        }
    }
}

It's the content of the file 这是文件的内容

2       helloworld                    10173.991234
124     helloworld                    10173.991234
125     helloworld                    10173.991234

Program arguments, for example: 程序参数,例如:

-c helloworld 10173.99 1234

I found what the problem was :) It was in UTF-8 coding. 我发现问题是什么:)它采用UTF-8编码。 I understood it after starting to use Notepad++ application. 我开始使用Notepad ++应用程序后就明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM