我目前正在编写一个测试素数的小应用程序。它是基于 gui 的,但我遇到了一个问题。我在程序中添加了一些限制,用户只能使用 numberformatexception 处理程序输入数字,但每当用户输入超过 9 位长度的数字时,它不再将其视为数字。这个问题有解决方案吗?我在下面留下了我的代码。
static void validation() // This is what happens when the "Check" button is clicked
{
// Retrieve information from the fields and print it out on the Frame
if (jtfX.getText().trim().length() == 0) // Check if the field is empty
{
jlSolution.setText("You have not entered anything yet");
}
else // Otherwise...
{
try // In general....
{
if (Long.parseLong(jtfX.getText()) < 0) // Check if it is a negative value
{
jlSolution.setText("The number you entered is less than zero");
}
else // If it isn't...
{
jlSolution.setText(new Algorithm(Integer.parseInt(jtfX.getText())).check()); // ....then check if this number is prime.
}
}
catch (NumberFormatException nfe) // ... always catch those who refuse to follow simple rules!
{
jlSolution.setText("Numerical values only please. " + "You entered: " + jtfX.getText());
}
}
}