-1

So my Eclipse works fine, it compiles everything I need. Now I just wrote code but it's not running it, it just doesn't do anything. I've tested Eclipse with other code and it works fine, so there is something wrong with my code, but I'm not sure what.

I am trying to complete Project Euler's 4th challenge:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Here's my code:

package me.practice;

public class Main{  

public static void main(String[] args)
{
    int palindrome = 0;

    for(int i = 0; i < 900; i++)
    {
        int num = (100 + i) * 999;

        String numString = Integer.toString(num);

        String numString_reverse = new StringBuilder(numString).reverse().toString();

        if(numString == numString_reverse)
        {
            palindrome = Integer.parseInt(numString);

            System.out.println(palindrome);

            break;
        }
    }
}
}

Edit: I've also tried to run it on Netbeans and it doesn't work, so Eclipse is fine.

ClashClown
  • 63
  • 1
  • 7
  • 2
    where's the rest of the code? You can't just run loose functions, they need to be inside a class that can be compiled. – Mike 'Pomax' Kamermans Aug 13 '14 at 00:12
  • Maybe you have not used a class to wrap your main method. – Juned Ahsan Aug 13 '14 at 00:13
  • 3
    Regarding `"...so Eclipse is fine"`, of course it's fine A good rule of thumb to remember: if you think that the bug is in Java or Eclipse or something similar 99.99% of the time you (and I) will be wrong. – Hovercraft Full Of Eels Aug 13 '14 at 00:24
  • 2
    Can you add an else statement to print a message if `numString == numString_reverse` returns false? That way you can see if your code is just not ever finding a palindrome rather than not running at all. – takendarkk Aug 13 '14 at 00:33
  • Also, be sure to read [How to compare Strings in java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – takendarkk Aug 13 '14 at 00:36
  • Okay so I added an else statement and that else statement got ran. – ClashClown Aug 13 '14 at 00:44
  • @ClashClown my guess is int num = (100 + i) * 999; does not even produce any palindrome at all between the range you specified – Kick Buttowski Aug 13 '14 at 00:53
  • @ClashClown why you use (100 + i) * 999 to produce your num? – Kick Buttowski Aug 13 '14 at 00:55
  • @ClashClown as i told you, your formula does not produce palindrome for some reasons and I donot why you are using it. when I change 100 to 110 or 200, I see palindrome in the outcome – Kick Buttowski Aug 13 '14 at 01:02

2 Answers2

0

Replace

if(numString == numString_reverse)

with

if(numString.equals(numString_reverse))

The first if statement is comparing the string references (and they'll be false every time), rather than their content - which is what the second if statement here above does.

watery
  • 5,026
  • 9
  • 52
  • 92
  • Okay I did that, but it stills seems to not run. – ClashClown Aug 13 '14 at 00:18
  • I don't know the challenge you're talking about, are you sure there's a palindrome in your number sequence? Add an `else` statement to the innermost `if`, you should see all the discarded numbers. – watery Aug 13 '14 at 00:23
0

The Problem lies with your code: The if block is never executed, as the largest palindrome obtained by multiplying two 3-digit numbers is 906609 which is obtained when 993 is multiplied by 913

The previous solution given below is a wrong one and I am sorry about that:

package stackoverflow;

public class Palindrome {
    public static void main(String[] args)
    {
        int palindrome = 0;

        for(int i = 101; i < 999; i++) {
            for(int j=101; j<999; j++) {
            System.out.println("inside for loop");
            int num = i * 999;

            String numString = Integer.toString(num);

            String numString_reverse = new StringBuilder(numString).reverse().toString();

            if(numString.equals(numString_reverse)) {
                System.out.println("inside if block");
                palindrome = Integer.parseInt(numString);

                System.out.println(palindrome);

                break;
            }
            }
        }
    }   
}

And Here is the true solution:

package stackoverflow;

public class Palindrome2 {

    public static void main(String[] args) {
        int palindrome;
        for(int i=900; i<999; i++) {
            for(int j=901; j<999; j++) {
                int value=i*j;
                String result=Integer.toString(value);
                String revResult=new StringBuffer(result).reverse().toString();
                if(result.equals(revResult)) {
                    System.out.println(i);
                    System.out.println(j);
                    palindrome=Integer.parseInt(result);
                    System.out.println(palindrome);
                }
            }
        }
    }
}
Amitesh Rai
  • 866
  • 11
  • 21