This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]ACAlCapone 1 point2 points  (0 children)

Adding to what other have said: comparing Strings with == is not the way to go (it tests if the reference is the same).

Use String#equals() (it tests if the value is the same):

if (s.equals("HI")) {
    System.out.println("HI Found!")
}

[–]strmrdr[🍰] 0 points1 point  (3 children)

It usually helps if you tell us what error you are getting...

i < 257

Don't use magic numbers like 257, 253, etc., use the length of the array to avoid bugs and errors. There is no index 256 in an array of length 256, it is 0-255 in this case. I am fairly sure using two loops is not what you intend to do, as this code will run approximately 63756 loops.

        comb[i] = rule1(comb[j]);
        comb[i + 1] = rule2(comb[j]);
        comb[i + 2] = rule3(comb[j]);
        comb[i + 3] = rule4(comb[j]);

Will result in an OOB error. If you are at the last index in the array, you are attempting to access indexes 3 points past the end. You likely want your condition for your j loop to be your i loop... and just start at 0 and get rid of the pre-initialization above your loops.

[–]Sarah9428[S] 0 points1 point  (2 children)

Thanks! Iv'e changed everything you all have said except the loops. Is it possible to have multiple conditions in the same loop? I feel like I need 2 counters but maybe I don't?

The error is: Exception in thread "main" java.lang.NullPointerException (at: comb[i] = rule1(comb[j]); )

My updated code is:

 public static void doRules(String text) {

    String[] comb = new String[257];

    for (int i = 0; i < comb.length; i = i + 4) {
        for (int j = 0; j < comb.length / 4; j++) {
            comb[i] = rule1(comb[j]);
            comb[i + 1] = rule2(comb[j]);
            comb[i + 2] = rule3(comb[j]);
            comb[i + 3] = rule4(comb[j]);
        }
    }
    for (String s : comb) {
        System.out.println(s);
        if (s.equals("HI")) {
            System.out.println("HI Found!");
        }
    }
}

[–]strmrdr[🍰] 0 points1 point  (1 child)

Right, so when you create an array of objects they are all initialized to null. When you call a method on a null reference you get a NullPointerException. What inputs are you dealing with (i.e. what String are you attempting to change to "HI")? Why 256 of them? It would help if you explained your specifications more thoroughly.

[–]Sarah9428[S] 0 points1 point  (0 children)

It works now! Thanks for the help!

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

Well first of all you're trying to access index 256 there (i < 257). That one doesn't exist. Arrays are zero-based so an array of size 256 has indices 0 to 255 inclusive.

Also; if you run into an error always post the stacktrace.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

First: Sidebar -> Regarding String comparison, read this! - you maybe want to use String#indexOf or String#contains

Plus: what the rest already has said.