views:

146

answers:

5

Hi,

i want to test if current char current is not ',', '-', '.' or ' ' Is there a shorter expression for:

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

any ideas?

EDIT:

I am just allowed to use the methods nextChar and getChar. I have to loop through the chars.

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;
    }
}
+13  A: 

Try

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

I cleaned it up to appear a bit nice, but if you're really after short then all you need is:

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

EDIT:

You can still use this approach even if you are limited to getChar() and hasNextChar()

while(hasNextChar()){
    char current = getChar();
    if (",-. ".indexOf(current) > -1) {
        wordCount++;
    }
    previous = current;         
}
Synesso
Simple and clear, +1.
Adam Rosenfield
sorry i missed it in my description but I am just allowed to use the methods nextChar and getChar. I have to loop through the chars.
ArtWorkAD
No worries ArtWorkAD. Updated my answer to match your revised question.
Synesso
"., " is string and input is char, I get an error
ArtWorkAD
Ah, sorry. I tested it in Scala, which must be doing an implicit conversion. Edited my answer.
Synesso
`indexOf` would work with `char`, no need to convert to String; if allowed...
Carlos Heuberger
Nice thinking Carlos!
Synesso
A: 

or if you wanted to be ridiculous put the chars in a static BitSet, and use isSet(current).

Unless this code is going to be executed millions of times, i'd stick with what you have, as code clarity is more important than unmeasurable performance gains.

MeBigFatGuy
+3  A: 

If it is an entire string you are looping through you might want to consider using regular expressions.

An example of validating with regex: http://stackoverflow.com/questions/878715/checking-string-for-illegal-characters-using-regular-expression

The example is white-listing character rather than blacklisting. This would in most cases be the preferred option as there is far more legal character-ranges than illegal.

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

Using Google Guava:

    CharMatcher unwantedChars=CharMatcher.anyOf("-,. ").precomputed();
    unwantedChars.apply(',');//true if unwanted char else false
Emil
+2  A: 

if you are not allowed to use String.indexOf like in:

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

use a switch like in

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

(instead of saving the previous char, you could just use a boolean to indicate if the previous char was a word boundary or a legal word character)

Carlos Heuberger
thanks, but e.g. I have "Thank You" and it would count just 1 word because "Thank You" has no character at the end that I could use to identify the end...
ArtWorkAD
@ArtWorkAD - that is another problem, not what was asked... but easy to solve as `previous` or the boolean is declared outside the loop, you can check it to decide if there is an additional word after the loop ended.
Carlos Heuberger