Originariamente inviato da cristal
Ho provato a fare:
codice:
String countOfStang = "|123|456|789|";
int numeroOccorrenze = 0;
for(int j = 0; j<countOfStang.length(); ++j) {
if (countOfStang.charAt(j)=="|") {
++numeroOccorrenze;
}
}
ma ho null point exception.
Cosa ho sbagliato?
1. charAt() restituisce un carattere e tu lo stai confrontando con una stringa. Risultato: incomparable types: char and java.lang.String. Sostituisci "|" con '|'
2. Dove avresti la NullPointerException? Io ho provato il tuo codice e funziona correttamente:
codice:
public class Test {
public static void main(String[] args) {
String countOfStang = "|123|456|789|";
int numeroOccorrenze = 0;
for(int j = 0; j < countOfStang.length(); ++j) {
if (countOfStang.charAt(j)=='|') {
++numeroOccorrenze;
}
}
System.out.println("occorrenze = " + numeroOccorrenze);
}
}