Netbeans tells me to remove the null comparison, but that breaks my code

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

He produces this conclusion:

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 goal of the null line is that I do not want to rest before starting the hunt for the first cat. Netbeans highlights the line, saying I have to delete it.

netbeans highlighting

So I'm changing

            if (catLevel != null)
              rest();

to

          rest();

But now I am dying:

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?

+4
source share
5 answers

Why nulldoes my application break when I delete the check ?

First of all, the following should be noted: Netbeans warning is incorrect.

By removing the check nullaround rest();, you do while. , rest(). , hunger , , null .

:

  • : hunger = 4, level = 3
  • rest() : hunger 5.
  • findCat() : hunger 6, "1.Ginger".
  • huntCat(1) : hunger 7.
  • 1 < 3 (catLevel < level) true, hunger 4 ( 3).
  • hunger 5, .
  • : hunger = 3, level = 3
  • rest() , hunger 4.
  • findCat(), hunger 5, "3.Porkchops".
  • huntCat(3) , hunger 6
  • 3 < 3 (catLevel < level) false, hunger .
  • hunger , 5, RuntimeException("x_x") .

?

, , Netbeans ( catLevel null ), null .

, , , " ". , .

level 4 hunger 3 if, , hunger > 6.

+4

, , :

Product Version: NetBeans IDE 8.0 (Build 201403101706)
Java: 1.8.0; Java HotSpot(TM) 64-Bit Server VM 25.0-b70
Runtime: Java(TM) SE Runtime Environment 1.8.0-b132
System: Windows 8 version 6.2 running on amd64; Cp1252; en_US (nb)

, NetBeans, .

, null, hunger 3, .

public class HuntigSaeson {
    int hunger = 3;

    ...
    void hunt() {
        Integer catLevel;
        do {
            rest();
            catLevel = new Integer(findCat().split("\\.")[0]);
            huntCat(catLevel);
            if (hunger > 5) throw new RuntimeException("x_x");
        } while (hunger > 0);
        System.out.println("^_^");
    }
    ...
}
+1

: Netbeans . Netbeans , , . . . Netbeans, /.

:

  • Netbeans.

  • " "

0

:

    void hunt() {
    try {
        while (hunger > 0) {
            huntCat(new Integer(findCat().split("\\.")[0]));
            if (hunger > 5)
                throw new RuntimeException("x_x");
            rest();
        }
    } catch (NoSuchElementException e) {
    }
    System.out.println("^_^");
}
0

NetBeans , , :)

if , rest() , , , 4.

, if, reset(), 5, , , 5, , 5 - .

, NetBeans, ( ), ( null).

.

-2

Source: https://habr.com/ru/post/1534172/


All Articles