Determing if an integer/string is a palindrome, and returning the reversed string/integer if it isn't

huangapple go评论70阅读模式
英文:

Determing if an integer/string is a palindrome, and returning the reversed string/integer if it isn't

问题

public class PalindromeChecker {

    public static void main(String[] args) {

        // ... (unchanged code for user input)

        if (userInput.equals("string")) {
            System.out.println("Enter the string now:");
            String s;
            s = keyboard.nextLine();

            if (isPalindromeString(s)) {
                System.out.println("Yes, the string is a palindrome.");
            } else {
                System.out.println("No, the string is not a palindrome.");
                System.out.println("The reverse of the string is: " + reverseString(s));
            }
        } else if (userInput.equals("integer")) {
            System.out.println("Enter the number now:");
            int i;
            i = keyboard.nextInt();

            if (isPalindromeInteger(i)) {
                System.out.println("Yes, the integer is a palindrome.");
            } else {
                System.out.println("No, the integer is not a palindrome.");
                System.out.println("The reverse of the integer is: " + reverseInteger(i));
            }

            keyboard.nextLine();
        }
    }

    public static boolean isPalindromeInteger(int a) {
        // ... (unchanged code for palindrome check)
    }

    public static boolean isPalindromeString(String s) {
        // ... (unchanged code for palindrome check)
    }

    public static int reverseInteger(int a) {
        // ... (unchanged code for integer reversal)
    }

    public static String reverseString(String s) {
        // ... (unchanged code for string reversal)
    }

    public static boolean isEven(int a) {
        // ... (unchanged code for checking evenness)
    }
}

Please note that the provided code is unchanged except for wrapping it within a class called PalindromeChecker. The class encapsulation helps organize the code and is good practice in Java.

英文:

I'm trying to to determine if a string or integer is a palindrome (same, if reversed). I've written a few methods, however, when I run the code, I keep getting an error for my isPalindromeString() and isPalindromeInteger() methods, saying the methods "must return a boolean type." I have setup clauses in the methods to return true/false based on my if statements. Not sure what I'm doing wrong here. Any guidance would be appreciated. Thanks. PS: I'm trying to not use any arrays, data structures and/or class methods.

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput;
System.out.println("Would you like to enter a string or integer?");
userInput = keyboard.nextLine().toLowerCase();
while(!userInput.equals("string") && !userInput.equals("integer"))
{
System.out.println("Invalid input. Try again. ");
userInput = keyboard.nextLine().toLowerCase();
}
if(userInput.equals("string"))
{
System.out.println("Enter the string now: ");
String s;
s = keyboard.nextLine();
if(isPalindromeString(s))
{
System.out.println("Yes, the string is a palindrome.");
}
else
{
System.out.println("No, the string is not a palindrome.");
System.out.println("The reverse of the string is: " + reverseString(s));
}
}
else if(userInput.equals("integer"))
{
System.out.println("Enter the number now: ");
int i;
i = keyboard.nextInt();
if(isPalindromeInteger(i))
{
System.out.println("Yes, the integer is a palindrome.");
}
else
{
System.out.println("No, the integer is not a palindrome.");
System.out.println("The reverse of the integer is: " + reverseInteger(i));
}
keyboard.nextLine();
}
}
public static boolean isPalindromeInteger(int a)
{
int counter=0;
String s = Integer.toString(a);
int reverseCounter = s.length()-1;
if(isEven(s.length()))
{
while(counter!=(s.length()/2))
{
if(s.charAt(counter)!=s.charAt(reverseCounter))
{
return false;
}
counter++;
reverseCounter--;
}
return true;
}
else if(!isEven(s.length()))
{
while(counter!= (s.length()/2))
{
if(s.charAt(counter)!=s.charAt(reverseCounter))
{
return  false;
}
counter++;
reverseCounter--;
}
return  true;
}
}
public static boolean isPalindromeString(String s)
{
int counter=0;
int reverseCounter = s.length()-1;
if(isEven(s.length()))
{
while(counter!=(s.length()/2))
{
if(s.charAt(counter)!=s.charAt(reverseCounter))
{
return false;
}
counter++;
reverseCounter--;
}
return true;
}
else if(!isEven(s.length()))
{
while(counter!= (s.length()/2))
{
if(s.charAt(counter)!=s.charAt(reverseCounter))
{
return false;
}
counter++;
reverseCounter--;
}
return true;
}
}
public static int reverseInteger(int a)
{
String s = Integer.toString(a);
String temp = new String();
for(int i = (s.length()-1); i >=0; i--)
{
temp = temp + s.charAt(i);
}
int num = Integer.parseInt(temp);
return num;
}
public static String reverseString(String s)
{
String temp = new String();
for(int i = (s.length()-1); i >=0; i--)
{
temp = temp + s.charAt(i);
}
return temp;
}
public static boolean isEven(int a)
{
if(a % 2 ==0)
return true;
else 
return false;
}

答案1

得分: 1

在你的 isPalindromeString()isPalindromeInteger() 方法中,你的 if/elif 代码块之外缺少了一个返回语句。只需在 else if 检查之后添加 return false,你的代码就应该可以工作。

英文:

In both your isPalindromeString() and isPalindromeInteger() methods, you do not have a return statement outside of your if/elif blocks. Simply adding a return false after your else if check should make your code work.

huangapple
  • 本文由 发表于 2020年8月29日 14:48:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定