Knowee
Questions
Features
Study Tools

class Solution { public boolean isPalindrome(int x) { String num =Integer.toString(x); for(int i=num.length()-1;i<0;i--){ String a =" "+num; if(num==a){ System.out.println("it is palindrome:"+a); }else{ System.out.println("it is not palindrome:"+a); } } }}

Question

class Solution { public boolean isPalindrome(int x) { String num =Integer.toString(x); for(int i=num.length()-1;i<0;i--){ String a =" "+num; if(num==a){ System.out.println("it is palindrome:"+a); }else{ System.out.println("it is not palindrome:"+a); } } }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code you've written is in Java and it's supposed to check if a given integer is a palindrome. However, there are a few issues with it. Here's a step-by-step explanation:

  1. You're converting the integer x to a string num. This is fine.

  2. You're running a loop from the end of the string to the beginning. However, the condition in your loop is incorrect. It should be i >= 0 instead of i < 0.

  3. Inside the loop, you're creating a new string a which is just num with a space in front. This doesn't make sense if you're trying to check for a palindrome.

  4. You're then comparing num and a with ==. In Java, == checks for reference equality for objects, not their contents. You should use .equals() to compare strings.

  5. You're printing out whether the number is a palindrome inside the loop. This should be done after the loop.

Here's a corrected version of your code:

class Solution {
    public boolean isPalindrome(int x) {
        String num = Integer.toString(x);
        int i = 0, j = num.length() - 1;
        while (i < j) {
            if (num.charAt(i) != num.charAt(j)) {
                System.out.println("It is not a palindrome: " + num);
                return false;
            }
            i++;
            j--;
        }
        System.out.println("It is a palindrome: " + num);
        return true;
    }
}

This code converts the integer to a string, then checks if it's a palindrome by comparing the characters at the start and end of the string, moving towards the center. If it finds any pair of characters that don't match, it prints out a message and returns false. If it gets through the whole string without finding any mismatched characters, it prints out a message and returns true.

This problem has been solved

Similar Questions

You are provided with a code to find if a given string is a palindrome or not.The code is shuffled.  Rearrange the code in the correct order.System.out.println(str+" is not a Palindrome");import java.util.Scanner;StringBuffer sb = new StringBuffer(str);public static void main(String[] args) {Scanner sc=new Scanner(System.in);public class Main {elseString str=sc.next();sb.reverse();if(str.equalsIgnoreCase(reverse))   }}System.out.println(str+" is a Palindrome");String reverse=sb.toString();

Given an integer x, return true if x is a palindrome, and false otherwise.

Given an integer x, return true if x is a palindrome, and false otherwise. Example 1:Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left.Example 2:Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Example 3:Input: x = 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.

Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.

Write a program in java to accept a number and chech whether it is a palindrome number or not. Do the program without using modulus operator and String.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.