0

私のcsクラスのこの課題の目標は、文字列から数字を検索し、それらを書面形式に変更することです

元:4 -> four

比較的単純な作業です。ただし、2つの問題があります。

1)現在のコードでは、「8」だけの文字列を変換して8」にしようとすると、現在の文字列の長さよりも長いため機能しません。

2) 文字列を通じて連続して複数の数値文字を処理する。なんとなく分かってきました。私が持っているものを確実に実行するとStrings、動作します。複数の数値文字はハイフンで区切ることになっています。

これが私のコードです:

public class NumberConversion {

    /**
     * * Class Constants **
     */
    /**
     * * Class Variables **
     */

    /* No class variables are needed due to the applet not having a state.
     All it does is simply convert. */
    /**
     * * Class Arrays **
     */
    char numberChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    String numbers[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

    /**
     * * Accessor Methods **
     */
    /**
     * * Transformer/Mutator Methods **
     */
    public void writeNumber(String phrase) {
        /**
         * Local Variables *
         */
        String newPhrase = "";
        int j = 0;
        int k = 0;

        phrase = phrase.trim();

        /**
         * * Counts through the length of phrase **
         */
        for (int i = 0; i < phrase.length(); i++) {
            /**
             * * If the current char is a number char, enter the next repitition
             * structure **
             */
            int l = i + 1;

            if (isNumber(phrase.charAt(i)) && isNumber(phrase.charAt(i + 1))) {
                boolean searchArray = true;

                do {
                    if (numberChar[ j] == phrase.charAt(i)) {
                        searchArray = false;
                    }

                    j++;

                } while (searchArray && j < numberChar.length);

                phrase = phrase.replace(Character.toString(phrase.charAt(i)), numbers[ j - 1] + "-"); //error HERE

            }

            if (isNumber(phrase.charAt(i))) {
                boolean searchArray = true;

                do {
                    /**
                     * * Counts through numberChar array to see which char was
                     * found in the phrase. Stops when found **
                     */
                    if (numberChar[ k] == phrase.charAt(i)) {
                        searchArray = false;
                    }

                    k++;

                } while (searchArray && k <= numberChar.length);

                /**
                 * * Changes char to string and replaces it with the matching
                 * String numbers array element **
                 */
                phrase = phrase.replace(Character.toString(phrase.charAt(i)), numbers[ k - 1]);
            }
            phrase = phrase.replace("- ", " ");
        }
        System.out.println(phrase); // Prints the changed phrase.
    }

    /**
     * * Helper Methods **
     */
    /**
     * * Observer Methods **
     */
    public boolean isNumber(char input) {
        boolean isNumber = false; // Initially fails

        for (int i = 0; i < numberChar.length; i++) {
            /**
             * * If input matches a number char, method returns true **
             */
            if (input == numberChar[ i]) {
                isNumber = true;
            }
        }
        return isNumber;
    }
}
4

3 に答える 3

0

現在の文字列の数字を置き換えたり、新しい文字列を作成したり、それに追加したりしないでください。これにより、多くの不要な検証を回避できます。

    List<Char> myNumberChar = Arrays.asList(numberChar);
    List<Char> myNumbers = Arrays.asList(numbers);
    StringBuilder mySB = new StringBuilder();
    //String myResultString = ""; // If you don't want to use StringBuilder
    for (int i = 0; i < phrase.length(); i++) {
        mySB.append(myNumbers.get(myNumberChar.getIndexOf(phrase.charAt(i)))+"-");
        //myResultString = myResultString + myNumbers.get(myNumberChar.getIndexOf(phrase.charAt(i)))+"-"; //Again, for no StringBuilder case
    }
    String myResult = mySB.toString();
    // No need to do the above in no StringBuilder case, just use myResultString in place of myResult
    System.out.println(myResult.subString(0, myResult.length - 1)); // removed the last hyphen
于 2013-04-26T02:40:59.330 に答える
0
    public class NumberConversion

{

/*** Class Variables ***/

/* No class variables are needed due to the applet not having a state.
   All it does is simply convert. */

/*** Class Arrays ***/

char numberChar[] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };

文字列 numberWord[] = { "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" };

/*** Transformer/Mutator Methods ***/

public String writeNumber( String phrase ) { /** ローカル変数 **/

  phrase = phrase.trim();

  /*** Counts through the length of phrase ***/

    for ( int i = 0; i < phrase.length(); i++ )
    {

     /*** If current char is a number, count through the array
          that contains the number chars until the one needed is
          found. ***/

        if ( isNumber( phrase.charAt( i ) ) )
        {
            boolean keepSearching = true;

            int j = 0;

            do
            {
               if ( numberChar[ j ] == phrase.charAt( i ) )
                  keepSearching = false;

               else
                  j++; // Increments j if char doesn't match array element

            } while ( keepSearching && j < numberChar.length );

            /*** Replaces the current char with the corresponding String
                 word from the other number array.  ***/

            phrase = phrase.replace( Character.toString(
                                      phrase.charAt( i ) ) ,
                                      numberWord[ j ] + "-" );
    }
    }

    /*** Gets rid of dashes from unwanted places ***/

    phrase = phrase.replaceAll( "- " , " " );
    phrase = phrase.replaceAll( "-," , ", " );
    phrase = phrase.replace( "-." , "." );

    if ( phrase.charAt( phrase.length() - 1 ) == '-' )
       phrase = phrase.substring( 0 , phrase.length() - 1 );

  return phrase;
}

/*** Observer Methods ***/

public boolean isNumber( char 入力 ) { boolean isNumber = false; // 最初は失敗

    for ( int i = 0; i < numberChar.length; i++ )
    {
        /*** If input matches a number char, method returns true ***/

        if ( input == numberChar[ i ] )
           isNumber = true;
    }

    return isNumber;
}
于 2013-04-28T23:52:09.847 に答える