Search code examples
eclipseclasspalindrome

check palindrome in java code


I'm making a really simple app in java code, but for some reason it doesnt work. its a palindrome checker.

here is the code.

MAIN:

public class main { public static void main(String[] args) { Palindroom.palindroomChecker("RACECAR"); } }

`Palindroom class:

public class Palindroom {

public static void palindroomChecker(String input) {
    String omgekeerd = "";
    boolean isPalindroom = false;
    int length = input.length();
    for(int i = 0; i < length; i++ ) {
        String hulp = "" + input.charAt(i);
        omgekeerd = omgekeerd + hulp;
    }
    System.out.println(omgekeerd);
    System.out.println(input);
    if(omgekeerd.equals(input)){
        System.out.println("DIT IS EEN PALINDROOM!");
    }
    else {
        System.out.println("HELAAS, DIT IS GEEN PALINDROOM!");
    }
}

}`

For some reason the check in the if-statement doesnt go as it has to go. As you can see i checked omgekeerd and input and i also checked earlier the length of omgekeerd to see if there were clear spaces.

Can someone help me out!

thanks in advance

greetings Mauro Palsgraaf


Solution

  • You are not actually reversing the string, looks like omgekeerd will be in the same order as input.

    Replace for with for(int i = length-1; i >= 0; i--) {