2010-10-28 20 views
3

我想测试当前char 当前是不是',',' - ','。'或 '' 是否有一个较短的表达式:短条件下,java

if((current != ' ') || (current != '.') || ...) 

什么想法?

编辑:

我只允许使用方法nextChar和getchar函数。我必须通过字符循环。

import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class WoerterZaehlen { 
    public static void main(String[] args) { 
     int wordCount = 0; 
     /* Ab hier dürft ihr eigenen Code einfügen */ 
     char previous = ' '; 
     while(hasNextChar()){ 
      char current = getChar(); 
      if(current !=) 
      //if(((current == ' ') || (current == '.') || (current == ',')) && ((previous != ' ') && (previous != ','))){ 
      // wordCount++; 
      //} 
      previous = current;   
     } 
     /* Ab hier dürft ihr nichts mehr ändern. */ 
     System.out.println("Anzahl der Wörter: " + wordCount); 
    } 

    private static InputStreamReader reader; 
    private static int next = -1; 

    public static boolean hasNextChar() { 
     if(next != -1) 
      return true; 
     try { 
      if(reader == null) 
       reader = new InputStreamReader(new FileInputStream("textdatei.txt")); 
      next = reader.read(); 

     } catch (IOException e) { 
      System.out.println("Datei wurde nicht gefunden."); 
     } 
     return next != -1; 
    } 

    public static char getChar() { 
     char c = (char) next; 
     next = -1; 
     return c; 
    } 
} 
+0

我想你的意思是&&而不是||。 – 2010-10-28 05:33:26

+1

你为什么用“优化”来标记?最短的表达式在性能方面可能不是最优的。 – 2010-10-28 05:36:11

+0

我看到了你的编辑。我认为你需要使用Scanner类。它已经具有nextChar和getChar的功能。 – Emil 2010-10-28 06:37:26

回答

2

,如果你不能使用String.indexOf像:

if (" .,".indexOf(ch) != -1) { 
     /* do something */ 
    } else { 
     /* do if none of the above */ 
    } 

使用交换机像

switch (ch) { 
     case ' ': case '.': case ',': /* do something */ break; 
     default: /* do if none of the above */ break; 
    } 

(而不是保存以前的字符,你可以使用布尔值来指示前一个字符是字边界还是法律字符字符)

+0

谢谢,但例如我有“谢谢你”,它只会计算1个单词,因为“谢谢”没有字符,我可以用它来识别结尾...... – 2010-10-28 15:03:29

+0

@ArtWorkAD - 这是另一个问题,不是什么问题。 。但是如果在循环之外声明'previous'或布尔值很容易解决,您可以检查它以确定在循环结束后是否还有一个额外的单词。 – 2010-10-28 15:25:44

13

尝试

String prohibitedChars = ",-. "; 
boolean isProhibited = prohibitedChars.indexOf('-') > -1; 

我清理了出现一点很好,但如果你以后真的那么所有你需要的是:

",-. ".indexOf('-') > -1; 

编辑:

即使您仅限于getChar()和h(),您仍可以使用此方法。 asNextChar()

while(hasNextChar()){ 
    char current = getChar(); 
    if (",-. ".indexOf(current) > -1) { 
     wordCount++; 
    } 
    previous = current;   
} 
+0

简单明了,+1。 – 2010-10-28 05:43:28

+0

对不起,我错过了我的描述,但我只允许使用nextChar和getChar方法。我必须通过字符循环。 – 2010-10-28 06:29:13

+0

不用担心ArtWorkAD。更新我的答案以符合您的修订问题。 – Synesso 2010-10-28 08:20:47

0

或者如果你想成为荒谬的把字符放在一个静态的BitSet中,并使用isSet(current)。

除非这段代码要被执行数百万次,否则我会坚持你所拥有的,因为代码清晰度比无法衡量的性能收益更重要。

4
BitSet unwantedChars=new BitSet(); 
    unwantedChars.set('.'); 
    unwantedChars.set(','); 
    unwantedChars.set('-'); 
    unwantedChars.set(' '); 
    char current=','; 
    if(unwantedChars.get(current)) //true if unwanted char else false 
    { 
    //.... 
    } 

使用谷歌番石榴:

CharMatcher unwantedChars=CharMatcher.anyOf("-,. ").precomputed(); 
    unwantedChars.apply(',');//true if unwanted char else false 
0

因为许多人提供了其他解决方案,我会提供一个您可以使用但没有限制的解决方案。

String prohibitedChars = ",-. "; 
prohibitedChars.contains(char);