I have the following simple code to simulate cat hunting:
import java.util.Arrays;
import java.util.LinkedList;
public class HuntigSaeson {
int hunger = 4;
int level = 3;
LinkedList<String> cats = new LinkedList<String>(Arrays.asList(
"1.Ginger",
"3.Porkchops",
"2.Muffin",
"2.Max",
"1.Carrot",
"2.Puffy",
"1.Fatty"
));
void hunt() {
Integer catLevel = null;
do {
if (catLevel != null)
rest();
catLevel = new Integer(findCat().split("\\.")[0]);
huntCat(catLevel);
if (hunger > 5) throw new RuntimeException("x_x");
} while (hunger > 0);
System.out.println("^_^");
}
void rest() { hunger += 1; }
String findCat() {
hunger += 1;
String c = cats.pop();
System.out.println("found " + c);
return c;
}
private void huntCat(int catLevel) {
hunger += 1;
if (catLevel < level) {
System.out.println("chomp chomp chomp");
hunger -= 4;
}
}
public static void main(String[] args) { new HuntigSaeson().hunt(); }
}
It produces this output:
found 1.Ginger
chomp chomp chomp
found 3.Porkchops
found 2.Muffin
chomp chomp chomp
found 2.Max
chomp chomp chomp
found 1.Carrot
chomp chomp chomp
found 2.Puffy
chomp chomp chomp
found 1.Fatty
chomp chomp chomp
^_^
The intent of the null comparison line is that I don't want to rest before hunting the first cat. Netbeans highlights the line, saying I should remove it.
So I do, by changing
if (catLevel != null)
rest();
to
rest();
But now I die:
found 1.Ginger
chomp chomp chomp
found 3.Porkchops
Exception in thread "main" java.lang.RuntimeException: x_x
at HuntigSaeson.hunt(HuntigSaeson.java:24)
at HuntigSaeson.main(HuntigSaeson.java:46)
Why? How can I fix this?