0

I try to make a program which it can find palindromic number (it has to be pruduct of two 3-digits number and I hope that it contain 6 digit but it is not important). Here is my code:

public class palindromicNumber {
    public static void getPalindromicNumber() {
        boolean podminka = false;
        int test;
        String s;
        for (int a = 999; podminka == false && a > 100; a--) {
            for (int b = 999; podminka == false && b > 100; b--) {
                test = a * b;
                s = Integer.toString(test);
                int c = 0;
                int d = s.length();
                while (c != d && podminka == false) {

                    if (s.charAt(c) == s.charAt(d)) { // I think that problem is here but I can't see what
                        System.out.println(s);
                        podminka = true;
                    }
                    c++;
                    d--;
                }
            }
        }
    }
}

and if I want to compile it :

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:695)
at faktorizace.palindromicNumber.getPalindromicNumber(palindromicNumber.java:24)
at faktorizace.Faktorizace.main(Faktorizace.java:19)

Java Result: 1

5 Answers 5

2

There are two problems here:

  • You're starting off with the wrong upper bound, as other answers have mentioned
  • If c starts off odd and d starts off even, then c will never equal d. You need to use

    while (c < d && !podminka) // Prefer !x to x == false
    

Additionally, judicious use of break and return would avoid you having to have podminka at all.

As another aside, you've got a separation of concerns issue. Your method currently does three things:

  • Iterates over numbers in a particular way
  • Checks whether or not they're palandromic
  • Prints the first it finds

You should separate those out. For example:

public void printFirstPalindrome() {
    long palindrome = findFirstPalindrome();
    System.out.println(palindrome);
}

public long findFirstPalindrome() {
    // Looping here, calling isPalindrome
}

public boolean isPalindrome(long value) {
    // Just checking here
}

I suspect findFirstPalindrome would normally take some parameters, too. At this point, you'd have methods which would be somewhat easier to both write and test.

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

2 Comments

How can I find the largest palindrome ? I tried every palindromic number get to the array and the buble sort and then I called for array[0] but the compilator didn't give me resul ( it give me nothing :D ) Do you have any solution or idea ?
@user1551954: One question at a time... this sounds like a separate one, so ask a new question, after reading tinyurl.com/so-hints
1

String indices go from [0..length - 1]

Change int d = s.length(); to int d = s.length() - 1;

Update: As a quick aside, you are setting podminka to true when

s.charAt(c) == s.charAt(d)

If s = 100101 for example, you will terminate all of the loops on the first iteration of the while loop because the first and last characters are the same.

4 Comments

When you've changed 'd' update code that is related to this variable too. For instance comparison and conditional operators involved this variable.
Ouuu , that is the mistake what i'm looking for .It is easy to I figure it out when I know where I must look. :D
@user1551954: A quick fix could be setting podminka = true if the two characters are NOT equal. However, this would still terminate all of the loops. You should really take a look at @Jon Skeet 's answer.
So I figured out promblem ( I can find palindromic number ) but next problem appears now , I found pal. number but it isn't the largest one . I do what Jon Skeet show me so the problem is in : public long findFirstPalindrome() { // Looping here, calling isPalindrome }
0

int d = s.length();

An array of the strings chars will only go from 0 - length-1.

s.charAt(d) will always be out of bounds on the first iteration.

Comments

0

Take a look on JDK source code:

public char charAt(int index) {
    if ((index < 0) || (index >= count)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index + offset];
}

You can see that this exception is thrown when index is less then zero or exceeds the string length. Now use debugger, debug your code and see why do you pass this wrong parameter value to charAt().

1 Comment

You say exceeds, don't you mean meets or exceeds? After all, "hello".charAt(5) doesn't exceed the length, but throws an exception because it meets the length.
0
       public class palindromicNumber {
           public static void getPalindromicNumber(){
              boolean podminka = false;
               int test;
               String s;
            for(int a = 999;podminka == false && a>100; a-- ){
              for(int b = 999;podminka == false && b>100; b-- ){
                test = a*b;
                s = Integer.toString(test); 
                int c = 0;
                int d = s.length();
                while(c!=d && podminka == false){                          

                  if(s.charAt(c)==s.charAt(d - 1)){  
                    System.out.println(s);
                      podminka = true;                                
                }
                      c++;
                       d--;
                 }
                  } 

}

try this! string count starts from 0!

4 Comments

What does your inner loop do with a number such as 123?
I tried this ,error is gone but the result is unsatisfactory ( wrong :D ) . When I started it I got this : 990009 but it isn't palindromic number . Do you have any solution ?
@user1551954: Consider what happens with the algorithm when s = 990009. Follow the logic.
I think that it is correct . I take first character and compare to the last one . Then I take second character and compare to the penultimate one and over and over again . When I'm in the middle the algorithm stop and return final number .

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.