【问题标题】:remove int, char from string从字符串中删除 int、char
【发布时间】:2013-08-31 00:55:56
【问题描述】:

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

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

原贴的代码很粗糙,已经修复,下面我自己重新贴一下。

感谢您在这方面的所有时间和帮助!

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

【问题讨论】:

  • 不明白您的问题,但next.charAt(j) == nums[i] 在这里您将 int 与 char '1' == 1 进行比较,我认为这不是真的
  • 使用Integer.parseInt()静态方法将文本转换为int。
  • next = next.replace(next.substring(j,j), "");
  • @nachokk 编辑了上面的问题。
  • “我收到一条错误消息,提示我无法用字符串替换整数。” - 更具体一些。这是编译错误消息吗?它出现在哪条线上。 确切地说是什么意思。

标签: java arrays string methods


【解决方案1】:

你不会写

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

因为 .replace() 是仅用另一个字符串替换 String 的东西.. 甚至 char 也无法替换.. 所以你需要输入 cast..

所以我尝试并编辑了您的代码.. 让我知道你有什么问题...

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

}}}

【讨论】:

    【解决方案2】:
    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);
        }
    

    【讨论】:

      【解决方案3】:

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

      如果方法返回值(假设是int)那么它需要是

      public int word(String S){}
      

      如果方法没有返回任何值,那么它需要是

      public void word(String S){}
      

      如果要构造构造函数..构造函数名和类名需要相同..

      public World(String S){}
      

      【讨论】:

        猜你喜欢
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2012-04-22
        相关资源
        最近更新 更多