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