I am trying to create a program that will get input from a user using a package called 'bulletproof' (done, works fine) but I am getting an infinite loop error when I find a match. I am a bit over my head and I do not feel I quite comprehend why my code is not working. I am not looking for a direct answer as I am trying to learn this, but any help would be sincerely appreciated. Thank you, my code is as follows.
--edit-- i got the program working. Thank you all for your help, you folks are tremendous.
import bulletproof.*;
public class A26_1 {
public static void main(String[] args) {
BPScanner kb = new BPScanner();
String reservedWordToCheck = kb.getStringFromUser("Enter a word to see if it's reserved or enter leave: ");
System.out.println(ReservedWordChecker(reservedWordToCheck));
}
public static String ReservedWordChecker(String reservedWordToCheck) {
String[] table = {
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"
};
while (true) {
if (reservedWordToCheck.equalsIgnoreCase("leave"))
break;
boolean found = false;
for (int i=0; i < table.length; i++) {
if (reservedWordToCheck.equalsIgnoreCase(table[i])) {
found = true;
break;
}
if (found)
System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
else
System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");
}
System.out.println("OK BYE");
} return reservedWordToCheck;
}
}
I think easiest way to do is -
public static boolean ReservedWordChecker(String reservedWordToCheck) {
String[] table = {
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"
};
for (String data : table) {
if (reservedWordToCheck.equalsIgnoreCase(data)) {
return true;
}
}
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With