这样对麽,谢谢!
boolean inputNotOK=true;
while(inputNotOK)
{
   try
   {
      int integerVariable=Integer.parseInt(capacityEntered);
      inputNotOK=false;
   }
   catch(java.lang.Exception exception)
   {
      System.out.println("You should set a int number,Please try again");  
   }
}

解决方案 »

  1.   

    NumberFormatException - if the string does not contain a parsable integer.boolean inputNotOK=true;
    while(inputNotOK)
    {
      try
      {
          int integerVariable=Integer.parseInt(capacityEntered);
          inputNotOK=false;
      }
      catch(NumberFormatException exception)
      {
          System.out.println("You should set a int number,Please try again");  
      }
      

  2.   

    boolean inputNotOK = true; 
                while(inputNotOK )
                {
                   try 
                   {  
                        int integerVariable=Integer.parseInt(capacityEntered);
                        inputNotOK= false;
                   } 
                   catch (NumberFormatException exception)
                   { 
                        displayArea.setText( "You should set an int number, please try again!");
                   } // End try catch;
                   
                } // End while.
    结果是无限循环,在文本框内不挺的显示You should set an int number, please try again!"在catch中加入 break还出错,请指教,十分感谢
      

  3.   

    在catch把 inputNotOk=false;运行时也出错。
      

  4.   

    boolean inputNotOK = true; 
                while(inputNotOK )
                {
                  try 
                  {  
                       /******************************************/
                       //you must do something to change 
                       //   the value of "capacityEntered" !!!
                      
                        int integerVariable=Integer.parseInt(capacityEntered);
                        inputNotOK= false;
                  } 
                  catch (NumberFormatException exception)
                  { 
                        displayArea.setText( "You should set an int number, please try again!");
                  } // End try catch;
                  
                } // End while.
      

  5.   

    我想既然能输出You should set an int number, please try again就是捕获到了错误,1为什么不停的循环呢
      

  6.   

    boolean inputNotOK = true; 
                  while ( inputNotOK) 
                  { 
                       try 
                        { 
                                 int integerVariable= Integer.valueOf( capacityEntered.trim() ).intValue(); 
                                  inputNotOK = false; 
                        } 
                        catch ( java.lang.Exception exception)
                        {
                             displayArea.setText( "cannot understand, please try again!");
                         } // End try/ catch; 
                    } // End while. 
    还是不停循环
      

  7.   

    boolean inputNotOK = true; 
                  while ( inputNotOK) 
                  { 
                      try 
                        { 
                                int integerVariable= Integer.valueOf( capacityEntered.trim() ).intValue(); 
                                  
                        } 
                        catch ( java.lang.Exception exception)
                        {
                            inputNotOK = false;    
                            displayArea.setText( "cannot understand, please try again!");
                        } // End try/ catch; 
                    } // End while. 
      

  8.   

    不循环了,但是出现错误拉
     可能是int integerVariable= Integer.valueOf( capacityEntered.trim() ).intValue(); 
    写的不对,我输的是f测试
    Exception occureed during event dispatching:
    java.lang.NumberFormatException:f
                at java.lang.Integer.parseInt(Unknown Source)
                at java.lang.Integer.parseInt(Unknown Source)
                at PlayListGUI.actionPerformed(PlayListGUI.java:116)
                at java.awt.Button.processActionEvent(Unknown Source)
    .......................
      

  9.   

    parseInt()试试!
    我记不清了
      

  10.   

    int integerVariable=Integer.parseInt(capacityEntered);
    编译通得过,运行时错误一样,我实在不知道怎么写这句,谁能帮帮我
      

  11.   

    if(e.getSource()==capacityButton)
    {
    String capacityEntered=capacityField.getText(); // if capacity not entered
    if(capacityEntered.length()==0)
    {
    displayArea.setText("Capacity of songs must be entered!");
    return;
    } try 
    {  
    int integerVariable=Integer.parseInt(capacityEntered);


    catch (NumberFormatException exception)

    displayArea.setText( "You should set an int number, please try again!");
    return;
    }// End try catch; if(integerVariable<1)
    {
    displayArea.setText("Unavailable capacity");
    return;
    }
                
    // if everything is okay then specify the capacity
    list=new PlayList(Integer.parseInt(capacityEntered));
    }