2

現在、素数をテストする小さなアプリを作成しています。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());

        }
    }

}
4

1 に答える 1

1

クラスがカスタム作成されたクラスであると仮定すると、Algorithimそのコンストラクターの整数引数を a に置き換えて、BigIntegerより大きな値を保持できます。

jlSolution次のようにフィールドを更新します。

Algorithm algorithm = new Algorithm(new BigInteger(jtfX.getText()));
jlSolution.setText(algorithm.check()); 
于 2012-10-27T15:58:22.880 に答える