2013-08-31 60 views
0

删除从文件读入的字符串中的特定字符和所有整数。从字符串中删除int,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) 
    { 
     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); 
     } 
    } 
+2

不明白你的问题,但'next.charAt(J)== NUM​​S [I]'这里你用一个char' '1' 比较一个int == 1'我认为并非如此 – nachokk

+0

使用'Integer.parseInt()'静态方法将文本转换为int。 –

+1

next = next.replace(next.substring(j,j),“”); –

回答

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

如果word是你的方法..然后它的无效宣告......为你创建你需要有一个返回类型..即任何方法,

如果方法返回值(INT想)那么它必须

public int word(String S){} 

如果该方法不返回任何值,那么它必须是

public void word(String S){} 

如果你想建立一个构造函数。该构造函数名称和类名称必须相同..

public World(String S){} 
0

你不能写

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

因为.replace()是什么,仅替换字符串用另一个字符串..连字符无法替代..所以你需要强制类型转换..

所以我尝试和编辑您的代码.. 让我知道你有一些问题...

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

}}}