Short condition, Java

I want to test whether the current char is', ',' – ',' Or‘‘

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

Any ideas?

Edit:

I'm just allowed to use the methods nextchar and getchar I have to traverse the 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;
    }
}

Solution

If you don't allow string Indexof, such as:

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

Use the switch in the image

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

(instead of saving the previous char, you can use Boolean values to indicate whether the previous char is a word boundary or a legal word character)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>