1

这是我的 java 类,我得到了NumberFormatException. 谁能帮我?我已经尝试了很多次,但它一直有这个错误。

public class HelloFromHTML extends JApplet
{

    String name;
    String age, dimensions1 ,dimensions2;
    int num;
    Font f;

    public void init()
    {   
        f = new Font("TimesRoman", Font.BOLD,30);

        String a = this.getParameter("ageHTML");
        num = Integer.parseInt(a);      

        name = "Hello " + getParameter("nameHTML") + " " + num + " years'"; 
        dimensions1 = "JApplet size is " + getParameter("width") + ", " +  getParameter("height");  
        dimensions2 = "size is " + String.valueOf(getWidth()) + ", " +  String.valueOf(getHeight());
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        g.setFont(f);
        g.setColor(Color.black);

        //////////////////////////////////
        //   Display String
        g.drawString(name,5,50);
        g.drawString(dimensions1,5,100);
        g.drawString(dimensions2,5,200);
    }
}
4

1 回答 1

0

由于这条线num = Integer.parseInt(a);会导致它Exception被抛出,我建议你用一个try-catch块包围它。此外,调试您的代码以查看为什么a不是数字。catch对于任何Exception可能被抛出的东西,这是一个很好的做法。

try{
    num = Integer.parseInt(a);
}
catch(NumberFormatException e){
   //TODO: what happens when "a" is not a number
}
于 2012-12-03T09:04:09.103 回答