从字符串中删除int,char

时间:2013-08-31 00:55:56

标签: java arrays string methods

从文件中读取的字符串中删除特定字符和所有整数。

目标是从通过扫描仪读取的字符串中清除所有非句子结尾字符和数字。删除这些字符和整数的原因是从读入的单词中生成准确的单词计数,句子计数和音节计数。

原始发布的代码非常粗糙,已经修复,并将在我下面重新发布。

感谢您的所有时间和帮助!

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);
        }
    }

3 个答案:

答案 0 :(得分:0)

你不能写

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

因为.replace()是用另一个字符串替换String的东西..甚至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);  

}}}

答案 1 :(得分: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);
    }

答案 2 :(得分:-1)

如果word是您的方法..那么它是一个无效的声明...对于您创建的任何方法,您需要有一个返回类型..即,

如果方法返回值(假设为int),则需要

public int word(String S){}

如果方法没有返回任何值,则需要

public void word(String S){}

如果要构造构造函数,则构造函数名称和类名称必须相同..

public World(String S){}