NumberFormatException:对于输入字符串:" 2"

时间:2015-12-06 15:39:01

标签: java string

我有一个字符串列表。第一个要素是:

2       helloworld                    10173.991234

我已经编写了以下代码:

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

此代码首先解析第一个id字段并将其添加到arraylist。 但它在第idList.add(Integer.valueOf(subs));

行失败

问题是什么?有什么帮助吗?

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();
        }
    }
}

它是文件的内容

2       helloworld                    10173.991234
124     helloworld                    10173.991234
125     helloworld                    10173.991234

程序参数,例如:

-c helloworld 10173.99 1234

1 个答案:

答案 0 :(得分:1)

我发现问题是什么:)它采用UTF-8编码。我开始使用Notepad ++应用程序后就明白了。