2

I'm sure I'm making a rookie mistake with java(this is actually my first program). I am trying to port some working python code I made into java(as a learning/testing exercise to learn a bit of the differences) but I'm getting different results between the two.

My program takes a list of data and generates another list based on it(basically sees if a value can be broken down by a sum). Python correctly gives 2,578 results while Java only gives 12. I tried to find the same commands in java and thought I did but can't seem to figure out why the results differ(the difference between the two I have experienced problems with multi threading and syncing of variables, wasn't sure if Java was doing anything behind the scenes so I had a while loop to keep running until the results stabilize, but it didn't help). Any suggestions would be helpful.

Here's the offending code(java at the top, python and pseudo code commented out in the bottom as reference):

                        for (int c = 0; c <= max_value; c++){
                            String temp_result = (s - c  * data.get(i) +  "," + i);

                            if( results.contains( temp_result ) ){  
                                String result_to_add = (s + "," + i+1);
                                if( results.contains( result_to_add ) ){ 
                                    System.out.println("contains result already");
                                } else {
                                    results.add(result_to_add);

                                }    print len(T)

#Here's the basic pseudo code(I added a few control variables but here's a high level view):

for i = 1 to k
    for z = 0 to sum:
        for c = 1 to z / x_i:
            if T[z - c * x_i][i - 1] is true:
                set T[z][i] to true
*/
4
  • 1
    When you have a bug in your code and its doing something unexpected, the debugger can be very useful. Its usually the button next you Run in your IDE. ;) Commented Feb 23, 2012 at 13:01
  • @PeterLawrey Thank you, I use eclipse and have seen that button but never really clicked on it because in python a bunch of print statements and sys.exit() was often enough. I think I need a new debugging approach with java so I guess I'll have to learn what that button does. Commented Feb 23, 2012 at 13:12
  • Add a breakpoint on the line of interest and it will show you all the local variables. You can evaluate expressions so you can see what you would get if the code were different. This allows you try out fixes. Commented Feb 23, 2012 at 13:14
  • @PeterLawrey Thanks again, I'll look into what breakpoints are also. I have read two(basic) java books but my next book will be learning how to debug better. Seems like there are some great tools for debugging that I had no idea existed. Thank you Peter! Commented Feb 23, 2012 at 13:47

2 Answers 2

4

In java s + "," + i+1 is a String concatenation : "10" + "," + 4 + 1 will return 10,41.

Use String result_to_add = s + "," + (i+1); instead.

Sign up to request clarification or add additional context in comments.

2 Comments

The outer parentheses are redundant.
Thank you so much! It worked! didn't realize that was happening. I honestly spent like 2 hours looking everywhere else(I went so far as maybe behind the scenes cpu multiprocessing was causing variable to not be sync'ed..). Thanks so much!
1

I see you've solved it just now, but since I've written it already, here's my version:

This uses the trick of using a Point as a substitute for a 2-element Python list/tuple of int, which (coincidentally) bypasses your String concatenation issue.

public class Sums
{
    public static void main(String[] args)
    {
        List T = new ArrayList();
        T.add(new Point(0, 0));
        int target_sum = 100;
        int[] data = new int[] { 10, -2, 5, 50, 20, 25, 40 };
        float max_percent = 1;
        int R = (int) (target_sum * max_percent * data.length);
        for (int i = 0; i < data.length; i++)
        {
            for (int s = -R; s < R + 1; s++)
            {
                int max_value = (int) Math.abs((target_sum * max_percent)
                        / data[i]);
                for (int c = 0; c < max_value + 1; c++)
                {
                    if (T.contains(new Point(s - c * data[i], i)))
                    {
                        Point p = new Point(s, i + 1);
                        if (!T.contains(p))
                        {
                            T.add(p);
                        }
                    }
                }
            }
        }
        System.out.println(T.size());
    }
}

2 Comments

Thanks so much! It looks alot cleaner than mine and the similar flow makes me feel that my first java program wasn't as bad as I thought. Thanks again and I'll def. look up what the point command does.
java.awt.Point is a class that is actually intended for storing x,y coordinate pairs, but it serves quite well as a 2-element list of int. Bit of a hack, but probably no worse than using Strings ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.