0

ファイルから読み取った文字列から特定の文字とすべての整数を削除します。

目標は、スキャナーを介して読み取られている文字列から、文以外の末尾の文字と数字をすべてスクラブすることでした。これらの char と int を削除した理由は、読み込まれた単語から正確な単語数、文数、および音節数を生成するためでした。

元の投稿コードは非常にラフで、修正されており、私から以下に再投稿します。

いつもご協力いただきありがとうございます。

public class Word {
    private int wordCount, sentenceCount, syllableCount;
    private int nums [] = {1,2,3,4,5,6,7,8,9,0};
    private char vowels [] = {'a', 'e', 'i', 'o', 'u', 'y'};;
    private char punctuation [] = {'!', '?','.', ';', ':','-','"','(', ')'};;

    public Word()
    {
        wordCount = 0;
        sentenceCount = 0;
        syllableCount =0;
    }

    public Word(String next)
    {
        if(next.length() > 1)
        {
            //
            // Remove punctuation that does not create sentences
            //
            for(int i = 5; i < punctuation.length; i++)
                for(int j = 0; j < next.length(); j++)
                    if(next.charAt(j) == punctuation[i])
                            next = next.replace(j, "");
            //
            // Counting Sentences
            //
            for(int i=0; i < 5; i++)
                if(punctuation[i] == next.charAt(next.length()-1))
                    sentenceCount++;
            //
            // Remove numbers for accurate word counting
            //
            for(int i = 0; i < nums.length; i++)
                for(int j = 0; j < next.length(); j++)
                    if(next.charAt(j) == nums[i])
                        next = next.replace(j, "");
            //
            // Counts all syllables
            //
            for(int i = 0; i < vowels.length; i++)
                for(int j = 0; j < next.length()-1; j++)
                    if(vowels[i] == next.charAt(j))
                        syllableCount++;
            System.out.println(next);
        }
    }
4

3 に答える 3

0

あなたは書くことができません

next.return(j,"");// you are replacing the j value(int) with ""

.replace() は、文字列のみを別の文字列に置き換えるものであるため.. char を置き換えることはできません.. したがって、キャストを入力する必要があります..

だから私はあなたのコードを試して編集しました..あなたにいくつかの問題があることを知らせてください...

public class Word {
private int wordCount, sentenceCount, syllableCount;
private int nums [] = {1,2,3,4,5,6,7,8,9,0};
private char vowels [] = {'a', 'e', 'i', 'o', 'u', 'y'};
private char punctuation [] = {'!', '?','.', ';', ':','-','(', ')'};

public Word()
{
    wordCount = 0;
    sentenceCount = 0;
    syllableCount =0;
}

public Word(String next)
{
    System.out.println("The Given String is"+next);
    if(next.length() > 1)
    {
        int n=0;
        int a=0;
            if(next.charAt(0)=='"'){n=1;}
        if(next.charAt(next.length()-1)=='"'){a=1;}

        for(int y=0+n; y<next.length()-a;y++){
             if(next.charAt(y)=='"'){
                next=next.substring(0,y)+next.substring(y+1,next.length());
                                    }}
        for(int i=0;i<8;i++){
              char k=punctuation[i];
              String l= Character.toString(k);
              int j=next.indexOf(k);
              if (next.contains(l)) {next=next.replace(l,"");}}

        for(int i=0;i<10;i++){
              int k=nums[i];
              String q= Integer.toString(k);
              if (next.contains(q)){
                  next=next.replace(q, "");}}

System.out.println("The String After Editing is: "+next);  

}}}

于 2013-08-31T13:44:01.150 に答える
0
public class Word {
    private int wordCount, sentenceCount, syllableCount;
    private char vowels [] = {'a','e','i','o','u','y','A','E','I','O','U','Y'};
    private char punctuation [] = {'!','?','.',';',':','-','"','(',')',','};

    public Word()
    {
        wordCount = 0;
        sentenceCount = 0;
        syllableCount = 0;
    }

    public Word(String next)
    {
        // Remove numbers
        next = next.replaceAll("[0-9]", "");
        // Skip over blank tokens
        if(next.contains(" ") || next.length() < 1)
                return;

        // String builder method used for removing all non-sentence ending
        // grammar marks.
        StringBuilder newNext = new StringBuilder(next);
        String tempNext;
        if(newNext.length() > 0)
        {
            for(int i = 5; i < punctuation.length; i++)
                for(int j = 0; j < newNext.length(); j++)
                    if(punctuation[i] == newNext.charAt(j))
                    {
                         newNext = newNext.deleteCharAt(j);
                    }
            tempNext = newNext.toString();
            next = tempNext;

            if(next.length() < 1)
                return;
            else
                wordCount++;
        }

        // Count sentences
        for(int i = 0; i < 5; i++)
            if(next.charAt(next.length()-1) == punctuation[i])
                sentenceCount++;

        // Counts all syllables
        if(next.length() > 2)
        {
            for(int i = 0; i < vowels.length; i++)
                for(int j = 0; j < next.length()-1; j++)
                    if(vowels[i] == next.charAt(j))
                        syllableCount++;
        }
        //System.out.println(next);
    }
于 2013-09-03T17:00:55.147 に答える