Palindrome Check in Java using two Methods will not compile

223 views Asked by At

I was working on this for a class assignment, and I cannot for the life of me get it to compile. I keep getting this error:

CPT236PalindromeCheckMethod.java:52: error: reached end of file while parsing } ^ 1 error

I have tried adding, removing, and checking all of my braces, but I cannot find anything wrong with it. I am new to programming, so I figured fresh eyes might help me find the issue.

import java.util.Scanner;
public class CPT236PalindromeCheckMethod {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = input.nextLine();

        if (checkPalindrome(s))
            System.out.println(s + " is a palindrome");
        else
            System.out.println(s + " is not a palindrome");
    }

    public static boolean checkPalindrome(String str){

        String lowerString = str.toLowerCase();
        String resultString = "";

        for(int i=0; i<lowerString.length(); i++) {
            if(Character.isLetter(lowerString.charAt(i))) {
            resultString = resultString + lowerString.charAt(i);
       }
    }

    int low = 0;

    int high = resultString.length() - 1;

    boolean isPalindrome = true;
    while (low < high) {
        if (resultString.charAt(low) != resultString.charAt(high)) {
            isPalindrome = false;
            break;
    }

    low++;
    high--; 
  }

}

2

There are 2 answers

0
apollosoftware.org On BEST ANSWER

You need a return value in the second method. It needs to return true or false. Return isPalindrome.

You should ideally use SublimeText or Eclipse. That way it'll debug your text realtime with JAVAC, and tell you where your errors are. It's much easier to troubleshoot realtime.

import java.util.Scanner;

public class CPT236PalindromeCheckMethod {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String s = input.nextLine();

        if (checkPalindrome(s))
            System.out.println(s + " is a palindrome");
        else
            System.out.println(s + " is not a palindrome");
    }

    public static boolean checkPalindrome(String str) {

        String lowerString = str.toLowerCase();
        String resultString = "";

        for (int i = 0; i < lowerString.length(); i++) {
            if (Character.isLetter(lowerString.charAt(i))) {
                resultString = resultString + lowerString.charAt(i);
            }
        }

        int low = 0;

        int high = resultString.length() - 1;

        boolean isPalindrome = true;
        while (low < high) {
            if (resultString.charAt(low) != resultString.charAt(high)) {
                isPalindrome = false;
                break;
            }

            low++;
            high--;

        }
        return isPalindrome;
    }
}
0
mazhar islam On

Use IDE (like Eclipse, NetBeans...), so that you can easily indent your code.

Now put a } at the end of your file.

In method checkPalindrome() add return statement.

At the end of the while loop,

while (low < high) {
    if (resultString.charAt(low) != resultString.charAt(high)) {
        isPalindrome = false;
        break;
    }

    low++;
    high--;
}
return isPalindrome; // add this line

I added a return statement,

return isPalindrome;

and run your code, with

Input : Enter a string: madam

Outout : madam is a palindrome