0

I'm trying to make a program that finds the largest palindrome that is a product of two 3-digit numbers. This is what I have right now (I am new to programming):

    int num1 = 0;
    int num2 = 0;
    int product = 0;
    int tempProd1 = 0;
    int tempProd2 = 0;
    int tempProd3 = 0;
    int tempProd4 = 0;
    int tempProd5 = 0;
    int tempProd6 = 0;
    String prodCheck1 = "";
    String prodCheck2 = "";

    while (num1 < 1000){
        while (num2 < 1000){
            product = num1 * num2;
            prodCheck1 = Integer.toString(product);

            tempProd1 = product % 10;
            product = product / 10;
            tempProd2 = product % 10;
            product = product / 10;
            tempProd3 = product % 10;
            product = product / 10;
            tempProd4 = product % 10;
            product = product / 10;
            tempProd5 = product % 10;
            product = product / 10;
            tempProd6 = product % 10;
            product = product / 10;             

            prodCheck2 = "tempProd1" + "tempProd2" + "tempProd3" + "tempProd4" + "tempProd5" + "tempProd6";

            if (prodCheck1 == prodCheck2){
                System.out.println(prodCheck1);
            }   
            num2++;
        }
        num1++;
    }   

Thing is, every time I try to run it, it terminates without an error. Could someone explain what I'm doing wrong?

Edit: Thanks everyone, finally fixed it. The answer is 853358, if anyone was wondering.

Edit: Actually, the number was 906609.

Esomewa
  • 11
  • 3
  • Why are you comparing strings? (thats the wrong way to compare strings, but I'm not sure why you're doing it at all) – Richard Tingle May 12 '14 at 11:05
  • @RichardTingle There is a string comparison in the code above. – nanofarad May 12 '14 at 11:06
  • @RichardTingle I'm comparing strings to see if the product is the same forward as it is backwards. – Esomewa May 12 '14 at 11:07
  • 2
    @Esomewa in your code, `prodCheck2` is actually `tempProd1tempProd2tempProd3tempProd4tempProd5tempProd6`, instead of the concatenation of each of those variables' *content*. – assylias May 12 '14 at 11:08
  • Glad to here its sorted! Have you given debugging a go (stepping through the code line by line). It makes these sorts of issues much easier to solve – Richard Tingle May 12 '14 at 11:52

3 Answers3

1

One thing I noticed immediately is that after the first iteration of the inner loop, num2 is 1000 and so the inner loop will just do nothing in the remaining 999 iterations of the outer loop. You have to reset num2 to 0.

Also consider using "for" loops instead; they're designed to prevent this kind of mistake:

for (int num1=0; num1<1000; num1++) {
  ...
}

Another problem is that the palindrome check is wrong. You cannot compare Strings with == (it tests for object identity, not string equality -- you'd have to use equals() instead). But even that is wrong because prodCheck2 is "tempProd1tempProd2..." and doesn't contain the actual numbers. The easiest way to check for a palindrome would be:

if (tempProd1 == tempProd6 && tempProd2 == tempProd5 && tempProd3 == tempProd$) {
  ...
}
Martin Geisse
  • 1,189
  • 1
  • 9
  • 22
0
if (prodCheck1 == prodCheck2){
            System.out.println(prodCheck1);
}   

is a comparison that is based solely on the identity equality of prodCheck1 and prodCheck2. Rewrite that code as:

if (prodCheck1.equals()){
            System.out.println(prodCheck1);
} 

to use value equality that will return true for identical strings.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

Equals method for strings

There are several issues here. First == should not be used to compare strings. You should use string.equals(otherString);

Contatinating words

Second you appear to be combining words when you want to combine values

prodCheck2 = "tempProd1" + "tempProd2" + "tempProd3" + "tempProd4" + "tempProd5" + "tempProd6;

will give

prodCheck2 = "tempProd1tempProd2tempProd3tempProd4tempProd5tempProd6";

always. The fact that those words happen to have the same name as some of your variables makes no difference to java.

There are many better ways to concatenate integers. But the easiest is probably as follows

prodCheck2 = tempProd1 + "" + tempProd2 + "" +tempProd3 + "" +tempProd4 + "" +tempProd5 + "" +tempProd6";

Using while when for would be better

while (num1 < 1000){
              while (num2 < 1000){
              ......
              num2++;
        }
        num1++;
}

This code never decreases num2, which means num2 goes 1->1000 for num1=0 and then stays at 1000 from then on. I'm guessing this isn't what you want. We could fix the while loop but really this is what a for loop is for

for(int num1=0;num1<1000;num1++){
     for(int num2=0;num2<1000;num2++){
         //code as before, no need to inciment or reset num1 or num2 inside the loop
     }
}

Issues that don't break your code

You're declaring all your variables with very large scope. For example tempProd1 is declared outside all the loops depite only being needed inside the inner loop. Declare variables in the smallest scope possible. This will catch bugs like those we've found here. Critically num2 couldn't have accidently been made non resetting if you'd delared it within the first loop

Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
  • When I change the while loop to a for loop it gives me a duplicate local variable error? Edit: Nevermind, I just realized I declared the variables twice. – Esomewa May 12 '14 at 11:23