lundi 30 mars 2015

Scanning a file and matching strings Java ( Looking for a better way )

I am creating a simple java program which scans through a file and looks for specific strings, i have it working to some degree but i am sure that there is a much more efficient and better way of doing it, i am using an IF contains method to compare for many different strings however i feel that it is a bad way of practicing and was wondering if anyone could give me an idea of what else i could try?



while (sc.hasNextLine()) {

readLine = new Scanner(sc.nextLine());
x = readLine.nextLine();
numberOfLines++;


if (x.contains("a")) {
numberOfConsonants++;
}
if (x.contains("A")) {
numberOfConsonants++;
}
if (x.contains("e")) {
numberOfConsonants++;
}
if (x.contains("E")) {
numberOfConsonants++;
}
if (x.contains("i")) {
numberOfConsonants++;
}
if (x.contains("I")) {
numberOfConsonants++;
}
if (x.contains("o")) {
numberOfConsonants++;
}
if (x.contains("O")) {
numberOfConsonants++;
}
if (x.contains("u")) {
numberOfConsonants++;
}
if (x.contains("U")) {
numberOfConsonants++;
}
}

Aucun commentaire:

Enregistrer un commentaire