0

私はプログラミングにかなり慣れていないので、アルファベットの文字の順序を見つける簡単な練習プログラムに取り組んでいます。これは非常に簡単なはずですが、while ループを追加すると、何らかの理由で StringIndexOutOfBoundsException が発生します。そのため、プログラムは最初にすべきことを行います...しかし、プログラムを再実行しないと再度テストすることはできません。内部に単純な print ステートメントだけを使用して while ループをテストしたところ、うまくいきました。

どんな助けでも大歓迎ですありがとう!

import java.io.*;
public class test {


    public static void main(String[] args) throws IOException 
    {
        BufferedReader in;
        in = new BufferedReader (new InputStreamReader (System.in));
        boolean again=true;
        String response;

        while (again)
        {

            System.out.println("Enter a letter to find it's order in the alphabet");

            char theLetter = (char) in.read();

            System.out.println(theLetter + " is the " + convertLetter(theLetter) + "th letter of the alphabet");

            System.out.println("want to play again?");

            response = in.readLine();

            if (response.charAt(0)=='n')
            {
                again=false;
            }
        }

        System.out.println("end program");
    }


    public static int convertLetter(char TheLetter)
    {
        //number value 'a'=97
        //number value 'b'=98
        //number value 'c'=99

        //subtracting 'a' from any other number will reveal how many places away that number is from the start
        //thus finding it's chronological place in the alphabet

        int NumberValue= (int)TheLetter;
        int a = 'a';

        int CalulateOrder = (NumberValue - a) + 1; 

        return CalulateOrder;

    }
}
4

6 に答える 6

3

元の文字でEnterキーを押すと、read()を呼び出して1文字しか取得しないため、その改行はまだバッファ内にあり、Enterキーを押すとバッファに改行が残ります。したがって、readLine を呼び出すと、単純にその改行にヒットし、空の文字列が返されます。

最初に文字を要求されたときに、複数の文字を入力してこれをテストできます。readLine は空でない文字列を返すため、2 回目のループに進みます。

これを修正するには、元の read() を readLine() に変更して、Enter キーを押したために発生した改行を取得し、文字列から最初の文字を取得します。

これで修正されるはずです:

    import java.io.*;

public class test {

    public static void main(String[] args) throws IOException {
        BufferedReader in;
        in = new BufferedReader (new InputStreamReader (System.in));
        boolean again=true;
        String response;

        while (again) {
            System.out.println("Enter a letter to find it's order in the alphabet");
            response = in.readLine();
            if (response.length() > 0) {
                char theLetter = response.charAt(0);

                System.out.println(theLetter + " is the " + convertLetter(theLetter) + "th letter of the alphabet");
                System.out.println("want to play again?");

                response = in.readLine();
                if (response.length() > 0 && response.charAt(0)=='n') {
                    again=false;
                }
            }
        }
        System.out.println("end program");
    }

    public static int convertLetter(char TheLetter) {
        return (TheLetter - 'a') + 1;
    }

}
于 2012-07-12T19:01:10.317 に答える
3
 if (response.charAt(0)=='n') 

文字列が空の場合、""位置 0 に文字はありません。実行する前に確認してくださいcharAt()

于 2012-07-12T18:50:52.147 に答える
1

文字列インデックスにアクセスする唯一の場所は、if (response.charAt(0) == 'n')おそらく問題の領域です。

if(response.length() > 0 && response.charAt(0) == 'n')

トリックを行う必要があります。

編集: @TreySchroeder が指摘しているように、最初は行全体を読み取らないという点で、プログラムには別の問題があります。in.readLine();イニシャルの後に入れtheLetter = (char) in.read();て、この修正を他の問題に使用してください。

于 2012-07-12T18:53:48.660 に答える
1

犯人は、want to play again?プロンプトで「Enter」を押していることだと思います。は末尾のin.readLine();改行なしで行を返します ( javadocsを参照)。つまり、「Enter」を押すだけでは空の文字列が返されるため、最初の文字のチェック中に StringOutOfBoundException が発生します。

文字を確認する前に、空の文字列を確認します。

if(response.length() > 0 && response.charAt(0) == 'n')
于 2012-07-12T18:58:53.910 に答える
1

response nullまたはの長さは""? charその場合、インデックス 0を取得することはできません

于 2012-07-12T18:51:28.247 に答える
0
if (response.isEmpty() && response.charAt(0)=='n')

例外を回避します。

于 2012-07-12T19:03:35.040 に答える