0

First time posting here! Yeah I kind of just need some help with this assignment. We had to read an input file, go through a loop for each character in a line of the input file, and store that info in a char array, and then finally write an isPalindrome method and call it to determine if that line is a palindrome and print it to the console.

This is what I have so far, sorry if I didn't format correctly, it was kind of confusing. I'm still kind of a beginner and I'm not sure where I'm going wrong. My output is essentially "[C@91bee48 is not a palindrome!" over and over again with some variation on whether or not its a palindrome.

public class Palindromes {
    public static void main(String[] args) {
        int k = 0;

        try {
          Scanner inF = new Scanner(new File("palindromes.txt"));
          String aString;
          while (inF.hasNextLine()) {
            aString = inF.nextLine();
            k++;
            for (int i = 0; i < k; i++) {
              char[] phrases = new char[aString.length()];
              if (Character.isLetter(aString.charAt(k))) {
                phrases[i] = aString.charAt(i);
              }
              isPalindrome(phrases);
            }
          }
        }

        catch (FileNotFoundException e) {
          System.err.println("palindromes.txt not found!");
        }
    }


    public static void isPalindrome(char[] phrases) {
        boolean isPalindrome = false;
        int i1 = 0;
        int i2 = phrases.length - 1;
        while (i2 > i1) {
          if (phrases[i1] != phrases[i2]) {
            isPalindrome = false;
            System.out.println(phrases + " is not a palindrome!");
          } else {
            isPalindrome = true;
            System.out.println(phrases + " is a palindrome!");
          }
          i1++;
          i2--;
        }
    }
}
ansible
  • 3,569
  • 2
  • 18
  • 29
  • 2
    You can't use an array directly in `System.out.println`; that's why you're getting the weird `[C@91bee48` in the output. Replace `phrases` with `new String(phrases)`, which will convert the `char[]` to a `String`. – ajb Feb 13 '14 at 23:40
  • @Makoto I don't think it's a duplicate, because there are other problems besides the `toString` problem. – ajb Feb 14 '14 at 00:28

2 Answers2

3

Use Arrays.toString() to get the string representing the array.

For example:

System.out.println(Arrays.toString(phrases) + " is not a palindrome!");

Otherwise you get the default toString() for an array object - which is not what you are after.

amit
  • 175,853
  • 27
  • 231
  • 333
  • This is a `char[]` he's trying to print. `Arrays.toString` will return something like `"[H, e, l, l, o]"` instead of `"Hello"`. `new String(phrases)` will return `"Hello"`. But best would be to restructure the code or at least to pass the original `String` to the method, because it shouldn't be necessary to reconstruct a `String` that the program already has possession of. – ajb Feb 14 '14 at 00:32
0

The logic of your while loop is faulty. This kind of loop is a common idiom: you have to check a number of things, and if any check fails, you want the result to be false. If no check fails, you want the result to be true. The thing is, you can't tell if the result is true until the loop is completely finished. Your code is:

while (i2 > i1)
{
    if(phrases[i1] != phrases[i2])
    {
        isPalindrome = false;
        System.out.println(phrases + " is not a palindrome!");
    }
    else
    {
        isPalindrome = true;
        System.out.println(phrases + " is a palindrome!");
    }
    i1++;
    i2--;
}

The problem is that you're printing "is a palindrome" before you've checked all the characters, and you can't possibly know that. The way to write this kind of loop is something like this:

isPalindrome = true;
while (i2 > i1)
{
    if(phrases[i1] != phrases[i2])
    {
        isPalindrome = false;
        break;  // get out of the loop, it's pointless to continue
    }
    i1++;
    i2--;
}
// *** NOW you can check isPalindrome and display your output
ajb
  • 31,309
  • 3
  • 58
  • 84