Ryan
Ryan

Reputation: 359

Strings[][] getting the value of an int

I'm working on a project the has quest's, All the quest data is saved in the player files. My method of saving the quest data in the player file isn't much of a method but a simple string of ints.

What I've done in User.java:

private final int[][] questData = {{0}, {0}, {0}, {0}, {0}};

The above code is saving the level of progress on all 5 quest's.

In my QuestHandling.java i've written the code:

    public static void checkQuestStates(User user) {
        if (user.questProgress[1][1] == 0) {
        System.out.println("Quest One Status = 0");
        }
    }

This was a test to see if I could get the value of the first int in the string (User.java). Which leads to my question, How do I get the value of each int in the string & process it so I can do something like:

if (user.questProgress[1] == 0) {
        user.getPackets().sendConfig(29, 0); //(makes that quest (Quest #1) icon red a.k.a not started) 
}else if (user.questProgress[1] >= 1 && user.questProgress[1] != 10) {
        user.getPackets().sendConfig(29, 1); //(makes that quest (Quest #1) icon yellow a.k.a started
} else if (user.questProgress[1] == 10) {
        user.getPackets().sendConfig(29, 2); //(makes that quest (Quest #1) icon green a.k.a finished
}

Things I've changed & my errors:

what I changed: in Player.java (User.java)

public int[] questProgress = {0, 0, 0, 0, 0};

and I've also tried:

public int questProgress[] = {0, 0, 0, 0, 0};

in questHandler.java I've put:

public static void checkQuestStates(Player player) {
        if (player.questProgress[1] == 1) {
        System.out.println("Quest One Status = 0");
        }
    }

& my command to test it is

case "statustest":
            StartQuestManager.checkQuestStates(player);
            return true;

When I type the command (just this command) I get the following error:

ERROR! THREAD NAME: New I/O  worker #1
java.lang.NullPointerException
    at com.feather.game.player.quests.StartQuestManager.checkQuestStates(StartQuestManager.java:115)
    at com.feather.game.player.content.Commands.processAdminCommand(Commands.java:1239)
    at com.feather.game.player.content.Commands.processCommand(Commands.java:79)
    at com.feather.net.decoders.WorldPacketsDecoder.processPackets(WorldPacketsDecoder.java:1662)
    at com.feather.net.decoders.WorldPacketsDecoder.decode(WorldPacketsDecoder.java:279)
    at com.feather.net.ServerChannelHandler.messageReceived(ServerChannelHandler.java:98)
    at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:95)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:563)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:91)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:373)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:247)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Upvotes: 0

Views: 113

Answers (2)

Chris
Chris

Reputation: 1579

As people have already said, you probably don't want questData to be final since you will be changing it later.

Additionally, remember that arrays are zero-indexed, so

public static void checkQuestStates(User user) {
    if (user.questProgress[1][1] == 0) {
        System.out.println("Quest One Status = 0");
    }
}

Should be:

public static void checkQuestStates(User user) {
    if (user.questProgress[0][0] == 0) {
        System.out.println("Quest One Status = 0");
    }
}

I'm not entirely sure why you have an array of arrays, when the second dimension array only has a length of 1....would a regular array of ints work better for you? That way, you can simply do:

if (user.questData[0] == 0) {
    user.getPackets().sendConfig(29, 0); //(makes that quest (Quest #1) icon red a.k.a not started) 
} else if (user.questData[0] >= 1 && user.questData[0] != 10) {
    user.getPackets().sendConfig(29, 1); //(makes that quest (Quest #1) icon yellow a.k.a started
} else if (user.questData[0] == 10) {
    user.getPackets().sendConfig(29, 2); //(makes that quest (Quest #1) icon green a.k.a finished
}

If I'm reading your question wrong, can you please clarify (as I don't see any String[][] question)?

Upvotes: 1

Mike
Mike

Reputation: 894

EDIT: okay you've dropped the 2d array. lets do this again

private int[] questData = {0,0,0,0,0,0};

lets make this method a little more useful, and generic. Lets return a string. (it doesn't seem like you know how to use returns right now, as you've used void for all your methods)

public String checkQuestStates(User user, int questLine) {
    String sQuestData = "Quest 1 Status: "+Integer.toString(user.questProgress[questLine]);
    System.out.println(sQuestData);
    return sQuestData
    }

Upvotes: 1

Related Questions