現在、素数をテストする小さなアプリを作成しています。GUIベースですが、1つ問題があります。ユーザーが 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());
}
}
}