Brief conditions, java

I want to check if the current current is char ',', '-', '.' or '' Is there a shorter expression for:

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

any ideas?

EDIT:

I am just allowed to use the nextChar and getChar methods. I need to scroll characters.

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;
    }
}
+3
source share
6 answers

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

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

use the switch as in

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

(instead of storing the previous char, you can simply use a boolean to indicate whether the previous char was the boundary of a word or symbol of a legal word)

+2
source

Try

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

, , , , , :

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

EDIT:

, getChar() hasNextChar()

while(hasNextChar()){
    char current = getChar();
    if (",-. ".indexOf(current) > -1) {
        wordCount++;
    }
    previous = current;         
}
+13
    BitSet unwantedChars=new BitSet();
    unwantedChars.set('.');
    unwantedChars.set(',');
    unwantedChars.set('-');
    unwantedChars.set(' ');
    char current=',';
    if(unwantedChars.get(current)) //true if unwanted char else false
    {
     //....
    }

Google Guava:

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

, , .

:

, . , , .

+2

or if you want to funny put characters in a static BitSet and use isSet (current).

If this code fails to execute millions of times, I will stick to what you have, because code clarity is more important than immeasurable performance.

0
source

since many provide other solutions, I have provided one that you would use without restriction.

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

Source: https://habr.com/ru/post/1771862/


All Articles