尝试对字符串进行标记时的NumberFormatException

时间:2014-11-10 22:59:37

标签: java file-io runtime-error stringtokenizer

我试图将String标记化并将其保存到二进制文件中,但是当我运行该程序时,我得到一个NumberFormatException。这是我的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "  1"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Project6.saveBSAFile(Project6.java:187)

这是我的代码,它试图将String标记化并将其保存为二进制文件:

public void saveBSAFile() throws FileNotFoundException, IOException
    {
        jfc.setDialogTitle("Specify a file to save");
        int userSelection = jfc.showSaveDialog(this);

        if (userSelection == jfc.APPROVE_OPTION)
        {
            File filename = jfc.getSelectedFile();
            JOptionPane.showMessageDialog(null, "File to save " + filename,
                                         "Save Review", JOptionPane.INFORMATION_MESSAGE);

            FileOutputStream FOStream1 = new FileOutputStream(filename, true);
            DataOutputStream DOStream1 = new DataOutputStream(FOStream1);
        }
        else if (userSelection == jfc.CANCEL_OPTION)
        {
            return;
        }

        int index = 0;

        while (tools.getNumberOfItems() <= 10 && processRec.getToolRecords(index) != null)
        {   
            StringTokenizer tokens = new StringTokenizer(processRec.getToolRecords(index), "|:");

            toolStrTok = tokens.nextToken();
            toolNameTok = tokens.nextToken();
            idStrTok = tokens.nextToken();
            idTok = tokens.nextToken();
            qualStrTok = tokens.nextToken();
            qualTok = tokens.nextToken();
            stockStrTok = tokens.nextToken();
            stockTok = tokens.nextToken();
            priceStrTok = tokens.nextToken();
            priceTok = tokens.nextToken();

            idTok.trim();
            qualTok.trim();
            stockTok.trim();
            priceTok.trim();

            id = Integer.parseInt(idTok);
            quality = Integer.parseInt(qualTok);
            numInStock = Integer.parseInt(stockTok);
            price = Double.parseDouble(priceTok);

            FileOutputStream FOStream2 = new FileOutputStream(filename, true);
            DataOutputStream DOStream2 = new DataOutputStream(FOStream2);

            DOStream2.writeUTF(toolStrTok);
            DOStream2.writeUTF(" " + toolNameTok);
            DOStream2.writeUTF(" " + idStrTok + " ");
            DOStream2.writeInt(id);
            DOStream2.writeUTF(" " + qualStrTok + " ");
            DOStream2.writeInt(quality);
            DOStream2.writeUTF(" " + stockStrTok + " ");
            DOStream2.writeInt(numInStock);
            DOStream2.writeUTF(" " + priceStrTok + " ");
            DOStream2.writeDouble(price);
            DOStream2.close();

            index++;

        }//end loop 
    }//end saveBSAFile

这是我尝试进行标记化的字符串,它是从另一个类文件(通过调用processRec引用)中的方法中提取的:

public String getRecord(int index)
    {
        return "Tool Name:  " + toolArray[index].getName()
                + "| Tool ID:  " + toolArray[index].getToolID()
                + "| Tool Quality:  " + toolArray[index].getQuality()
                + "| Number in Stock:  " + toolArray[index].getNumberInStock()
                + "| Tool Price:  " + toolArray[index].getPrice();

    }//end getRecord

我尝试过一些不同的事情,例如使用Strings trim()尝试进行令牌化修剪{{1}},但这似乎不起作用:(我也尝试过重做这个代码有点但也没有给我带来太大的好运。在异常和堆栈跟踪方面,我非常非常新手,所以我希望有人能够指出任何明显的(或者不那么明显)我可能犯的错误。非常感谢提前:))

1 个答案:

答案 0 :(得分:2)

trim() method不会更改原始String;这是不可改变的。

  

返回一个字符串,其值为此字符串,删除了任何前导和尾随空格。

(强调我的)

trim方法返回修剪后的String,但您放弃了返回的StringidTok仍然是" 1",带有空格。

更改

idTok.trim();

idTok = idTok.trim();

和其他令牌一样。然后parseInt会看到修剪过的字符串(例如"1")并正确解析整数。