这样对麽,谢谢!
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");
}
}
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");
}
}
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");
}
}
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还出错,请指教,十分感谢
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.
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.
还是不停循环
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.
可能是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)
.......................
我记不清了
编译通得过,运行时错误一样,我实在不知道怎么写这句,谁能帮帮我
{
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));
}