Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Noobie in need of serious help

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Noobie in need of serious help

    Hello Everyone,
    I am trying to finish up my final assignment which is due in a few days and am confused on why the code I have is acting the way it is.

    This is a list of variables I have initialized at the top of the class.
        // variables used in saving game
        String UserDogCount;
        String UserWolfCount;
        int playerMoveCount;
        int totalPoints;
        int points;
        int level;

    This code is ONLY part of a method, which works perfectly,
            // for debugging ONLY
            System.out.println("Current Level # = " + level);
            System.out.println("Level Points = " + points);
            System.out.println("Total Points = " + totalPoints);
            System.out.println("Wolf's Found = " + userWolfCount);
            System.out.println("Sheep's Found = " + (level - 1));
            System.out.println("Dog's Found = " + userDogCount);
            System.out.println("Total # of moves = " + playerMoveCount);
            System.out.println();         
        } // end of newGrid method

    all variables above keeps perfect track of their values with no issues, I need to save the above variables to a CSV file, which I have written and it kind of works. Saves all values, except all values get recorded as '0'.

    So I create a temporary method for debugging purposes, you will notice that they are identical.
            public void resume(){  
     
            System.out.println("\n***************************************");
            System.out.println("Current Level # = " + level);
            System.out.println("Level Points = " + points);
            System.out.println("Total Points = " + totalPoints);
            System.out.println("Wolf's Found = " + userWolfCount);
            System.out.println("Sheep's Found = " + (level - 1));
            System.out.println("Dog's Found = " + userDogCount);
            System.out.println("Total # of moves = " + playerMoveCount);
            System.out.println("***************************************\n"); 
        }
    The above is in the exact same class as the top code, but yet when I call the second code, (via a button on my GUI console), all values show as '0'.
    I can not figure out why the variables are able to keep track in one method but not in the other.

    Here is a representation of what my screen looks like.
     

    1 2 3 4 5
    1 * S * * *
    2 * * * D *
    3 * * * * *
    4 * * * * *
    5 * * * W *
    Current Level # = 1
    Level Points = 25
    Total Points = 136
    Wolf's Found = 1
    Sheep's Found = 3
    Dog's Found = 1
    Total # of moves = 35


    ***************************************
    Current Level # = 0
    Level Points = 0
    Total Points = 0
    Wolf's Found = 0
    Sheep's Found = -1
    Dog's Found = 0
    Total # of moves = 0
    ***************************************



    BUILD SUCCESSFUL (total time: 1 minute 23 seconds)





    Can someone explain to me why this is happening. I've been working on this assignment for 10-12 hours a day, for the last 8 days. Not sure if my mind is fried, or if I am just too tired to see the error.
    Please help.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Noobie in need of serious help

    why the variables are able to keep track in one method but not in the other.
    Can you post all the code so it can be compiled and executed for testing?

    Are there two instances of the class holding the values? One instance has the good values, the other instance has 0s.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noobie in need of serious help

    Hi Norm, to post all the code would be a lot. There are 6 different classes working together, along with fxml class.
    I went through the entire class, line by line. Cannot, see them competing with another instance of itself.
    I'll go though it again, maybe I missed something. I'll get back with what I find (if anything).

    --- Update ---

    Hi Norm,
    Here's something that just confused me more, if I add one line to the first method
     
            // for debugging ONLY
            System.out.println("Current Level # = " + level);
            System.out.println("Level Points = " + points);
            System.out.println("Total Points = " + totalPoints);
            System.out.println("Wolf's Found = " + userWolfCount);
            System.out.println("Sheep's Found = " + (level - 1));
            System.out.println("Dog's Found = " + userDogCount);
            System.out.println("Total # of moves = " + playerMoveCount);
            System.out.println();
     
            resume(); // NEW LINE ADDED
        } // end of newGrid method

    then the second method's values work, however when the second method is called independently, it displays zero's.

    I'm wondering if somewhere, I screwed up and when the variables are assigned a value, they are not saved globally (even though I have instantiated them at the beginning of the class), so they can be retrieved by another method.

    public class SheepHerder {
        // creates new instances
        GameSetUp newLevel = new GameSetUp();
        StringBuilder moves = new StringBuilder();   
     
        //initializing points system
        int points;
        int totalPoints;
     
        // variables used in saving game
        String status; // is the game ended or pending   
        int playerMoveCount; // tracks the number of moves the player makes      
        int userWolfCount; // number of wolves found    
        int level; // current level number

    Here is what my screen looks like now,
     

    1 2 3 4 5
    1 * * * * *
    2 * * * * *
    3 S * * * *
    4 * HD * * *
    5 * * * * *
    Current Level # = 2
    Level Points = 33
    Total Points = 48
    Wolf's Found = 1
    Sheep's Found = 1
    Dog's Found = 1
    Total # of moves = 4


    ***************************************
    Current Level # = 2
    Level Points = 33
    Total Points = 48
    Wolf's Found = 1
    Sheep's Found = 1
    Dog's Found = 1
    Total # of moves = 4
    ***************************************


    ***************************************
    Current Level # = 0
    Level Points = 0
    Total Points = 0
    Wolf's Found = 0
    Sheep's Found = -1
    Dog's Found = 0
    Total # of moves = 0
    ***************************************




    The bottom set of values show the variables values when that method ( 'resume()' ) is called independently.

    Does this make any sense to you.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Noobie in need of serious help

    Sorry, it is very hard or impossible to see why the values are 0 without seeing the code.

    My guess is that there are two instances of the class. One with good values and one with 0s.
    How many instances of the class with the values in question are created?
    If you can't tell, add a print statement in the class's constructor that will print a message when it is called.
    Then the print out will show how many times it is called.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    CADman (August 12th, 2021)

  6. #5
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noobie in need of serious help

    Hi Norm,
    I do believe you are correct, I did the print statement in the constructor and found out and realized I have 2 classes instantiating that same class.
    I can pretty much guarantee that is were the issue is coming from, I am now working on how to figure out the easiest way to fix this. I may have to join the code from two classes into one, eliminating the second instantiation.

    Thanks Norm, your a genius, appreciate you time as always. Not sure if this will work, but I'll give it a go.

  7. #6
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noobie in need of serious help

    Hi Norm,
    Once again you hit the nail right on the head. Once I removed the second instance call, things fell right into place.
    Please accept my thanks for pointing me in the right direction.

Similar Threads

  1. noobie
    By Tathagata in forum Member Introductions
    Replies: 1
    Last Post: September 19th, 2013, 07:18 PM
  2. [SOLVED] Noobie equals() problem.
    By ViRAL in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 12th, 2011, 03:29 AM
  3. Noobie issues?
    By sukuiichuu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2011, 05:10 AM
  4. [SOLVED] Replace String Method? Noobie :/
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 18th, 2011, 10:58 AM
  5. Noobie to Java questions!! :D
    By chansey123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 11:53 AM