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 5 of 5

Thread: Why do these things fail sometimes? Annoying program!

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Why do these things fail sometimes? Annoying program!

    Hiya, I have been working on a program that looks for 4 in a line in a 2D array. I have made several methods such as
     if(token(i,j) == token(i+1,j) {
            if(token(i+1,j) == token(i+2,j) {
                      Counter++;
    }
    }
    These methods work fairly well sometimes and completely fail at others. I get all excited that my program works, and the next minute, using the same pattern, it doesnt! It also sees things that are not there.

    It this a common problem in java for these methods to not always check very well. Even my Winner method sometimes fails even though if I re-do the pattern it works. Also I have had to keep several backups as slight changes to it when working make it return very strange things!

    Why do my methods fail sometimes and not others? Is it my code, or some sort of slow java related problem?? I seem to get it working just by looking at it sometimes.

    Where token is just return array[col][row];
    Last edited by Scotty; April 30th, 2011 at 03:42 PM.


  2. #2
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Why do these things fail sometimes? Annoying program!

    Quote Originally Posted by Scotty View Post
    Hiya, I have been working on a program that looks for 4 in a line in a 2D array. I have made several methods such as
    [code]
    if(token(i,j) == token(i+1,j) {
    if(token(i+1,j) == token(i+2,j) {
    Counter++;
    }
    }

    These methods work fairly well sometimes and completely fail at others. I get all excited that my program works, and the next minute, using the same pattern, it doesnt! It also sees things that are not there. 1 doesnt not equal 0 or -1 as it sometimes thinks!

    It this a common problem in java for these methods to not always check very well. Even my Winner method sometimes fails even though if I re-do the pattern it works. Also I have had to keep several backups as slight changes to it when working make it return very strange things!

    Why do my methods fail sometimes and not others? Is it my code, or some sort of slow java related problem?? I seem to get it working just by looking at it sometimes.

    Where token is just return array[col][row];
    Does your code even compile?

    You are missing close parentheses on both your if conditions.

    Also could you provide more context?

    For instance what does token(type1, type2) do? What are its return type and parameter types?

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Why do these things fail sometimes? Annoying program!

    Sorry it was an example and not typed well. It returns an int
    public int token(int col, int row) {
    return array[col][row] }

    simply
    if (array[i][j] == array[i+1][j]) {
    if (array[i+1][j] == array[i+2][j]) {
     Counter += 3;
    }
    }
     
    if (array[i][j] == array[i][j+1]) {
    if (array[i][j+1] == array[i][j+2]) {
     Counter += 3;
    }
    }

    etc.

    And searches for different size lines of the same Number adding points accordingly.
    Is there a better way?
    Last edited by Scotty; April 30th, 2011 at 03:43 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why do these things fail sometimes? Annoying program!

    Why do my methods fail sometimes and not others? Is it my code, or some sort of slow java related problem??
    Java, or any other common programming language for that matter, would not be used worldwide in all sorts of distributed applications if it were unpredictable. Problems such as this are logic/algorithm related - in other words, its the programmers fault. It helps to think through the logic, draw out what happens, to help you debug. As is it is hard to see what's going on, so to help you break the problem down, try creating an SSCCE for both yourself and for us.

  5. #5
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Why do these things fail sometimes? Annoying program!

    Sounds like Connect 4.
    Something else seems to be at fault here, have you confirmed that the range of the loop variables, i and j, are correct? Hook us up with the code for the loops and the related variables that control the loops range.

Similar Threads

  1. A lot of annoying cast errors.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 10:54 PM
  2. XML, and other things.
    By Tortex in forum The Cafe
    Replies: 0
    Last Post: March 27th, 2010, 03:33 PM
  3. How would i do these things??
    By Curious in forum Java Theory & Questions
    Replies: 4
    Last Post: February 21st, 2010, 08:33 PM
  4. annoying error.
    By gajamaflake in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2010, 04:29 PM
  5. applet fail
    By wolfgar in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 21st, 2010, 06:24 AM