I am on euler project 4, the largest palendrome product of 2 3digit numbers.

Here is the program, however it doesn't print a value.

please help.

package euler;

public class problem004 {

    String reversed;

    public static void main(String[] args) {

        int product =0;
        String stringproduct;

        //cycles through all the numbers
        for(int i=999;i>=100;){

            for(int n=999;n>=100;){

                product = n*i;
                stringproduct = product +"";

                new problem004().reverse(stringproduct);

                //if the string is a palendrome
                if(stringproduct == new problem004().reversed){

                    System.out.println("answer is"+stringproduct);
                }

                n--;
            }
            i--;

        }

    }

    //reverses the string order

    void reverse(String rev){

        //""turns it to a string
        reversed = new StringBuffer(rev).reverse() + "";




    }

}
if(stringproduct == new problem004().reversed)

== for two objects tests if they are exactly the same object (same address in memory etc). In this case these are always two separate objects.
The String class has an equals(String another) method that tests if two Strings contain the same sequence of characters...

I agree with james, Thumbs up.
for example
String S1 = "Hello";
String S2 = "Hello";
String S3 = new String("Hello");
S1 and S2 they have one memory address with tow labels (S1 and S2) and they are equal, but S3 creates another address in the memory and for that reason S3 is different from S1 and S2 hence not equal.
So the best way is like what james posted. if (string1.equals(string2))...

I like the fact that since you are searching for the largest palendrome you started your nested loops with the largest int, so why not take advantage from that, you see your first print will be the answer and you don't need to keep the loop to display all the remaining results.
You should add a break; to stop the loop but since you have a nested loop you should label it.

TheLoop:
  for(int i=999;i>=100;i--){
    for(int n=999;n>=100;n--){
       product = n*i;
       stringproduct = product +"";
       new problem004().reverse(stringproduct);
       //if the string is a palendrome
          if(stringproduct == new problem004().reversed){ // edit here
             System.out.println("answer is"+stringproduct);
             Break TheLoop;
}}}

Good Luck

I am still having no output. I have used the .equals method but nothing.

package euler;

public class problem004 {

    String reversed;

    public static void main(String[] args) {

        int product =0;
        String stringproduct;

        //cycles through all the numbers
        for(int i=999;i>=100;){

            for(int n=999;n>=100;){

                product = n*i;
                stringproduct = product +"";

                new problem004().reverse(stringproduct);

                //if the string is a palendrome
                if(stringproduct.equals(new problem004().reversed)){

                    System.out.println("answer is"+stringproduct);
                }

                n--;
            }
            i--;

        }

    }

    //reverses the string order

    void reverse(String rev){

        //""turns it to a string
        reversed = new StringBuffer(rev).reverse() + "";




    }

}

line 20 you create a new instance and use it to reverse the string
line 23 you create another new instance and get the value from it.
The instance where you get the value is NOT the instance where you did the reversing.

In this case it would be simpler just to make the reverse method static.

oh thank you. I assumed because it was the same class it would change the variable.
this si the answer it printed:
answer is17271

However this is wrong.

at least it's a palindrome!

The whole point of instances of a class is that they all have their own individual values for all the instance (non-static) variables.

I tried printing out the numbers of n and i, it seemed to start on apparently random numbers.

I changed the loops to while loops and it sorte out that problem but now it doesn't get a palindome.

package euler;

public class problem004 {

    String reversed;

    public static void main(String[] args) {

        int product =0;
        String stringproduct;
        int i = 999;

        int n = 999;

        //cycles through all the numbers
        while(i>=100){

            //System.out.println("i equals" + i);

            while(n>=100){

                product = n*i;
                stringproduct = product + "";

                problem004 reversing0bject = new problem004();
                reversing0bject.reverse(stringproduct);

                //if the string is a palendrome
                if(stringproduct.equals(reversing0bject.reversed)){

                    System.out.println("answer is"+stringproduct);
                }
                //System.out.println("n equals" + n);

                n--;
            }

            i--;

        }

    }

    //reverses the string order

    void reverse(String rev){

        //""turns it to a string
        reversed = new StringBuffer(rev).reverse() + "";




    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.