Jowwy Scottsdale
Jowwy Scottsdale

Reputation: 31

Java Palindrome program not working

Hi I'm busy writing a program that checks (within terminal) if your input is a palindrome (sorry the Variabels are in Dutch) but the problem I have come on is even though I got the String reversed and in an if function when the input should match the reversed input (example: poop) but it still tells me instead: Nee, probeer opnieuw of ga naar huis(translation:No, try again or go home). Now I am home and how do I fix it that it gives accurate results?

Also I need to build a table out of arrays counting the a's showing their value, count b's... etc. until 9 and space could you maybe give me a push in the right direction for this too? (Not needed though really would like to know whats wrong with my function) public static void main (String[] args) {

String ui = ""; //ui = User Invoer
String palin = "";
String temp = "";
String answer = "";
int uiLength;
int klinkers = 0;
int woorden = 0;
int palinLength;
boolean zoektWoord= true; 

Scanner sui= new Scanner(System.in);
System.out.println("Voer een (echte) zin in:");
ui = sui.nextLine().trim();
uiLength = ui.length();
temp=ui.replaceAll( "[^A-Z a-z 0-9]", "");

    for (int q = 0; q< temp.length(); q++)
    {
        char aChar = temp.charAt(q);
            if (65 <= aChar && aChar<=90)
            {
                aChar = (char)( (aChar + 32) ); 
            }
        palin+=aChar;
    }

    for (int qw=0; qw<palin.length(); qw++)
    {
        if (palin.charAt(qw) == 'a'||
            palin.charAt(qw) == 'e'||
                palin.charAt(qw) == 'o'||
                palin.charAt(qw) == 'i'||
                palin.charAt(qw) == 'u')
            klinkers ++;
    }


    for (int x=0; x<palin.length(); x++)
    {       
        if (palin.charAt(x) == ' ') 
        {
        zoektWoord=true;
        }
    else{
        if(zoektWoord) woorden++;
        zoektWoord = false;

        }
    }
String nilap = new StringBuilder(palin).reverse().toString();
/*Als palin PRECIES gelijk is aan het omgekeerde DAN is het een palindroom*/
if (palin==nilap)
{
    answer="Jazeker, op de letter";
}
else 
{   
    answer="Nee, probeer opnieuw of ga naar huis";
}

    palinLength = palin.length();


System.out.println("Lengte ongefilterde zin: " + uiLength + " karakters" );
System.out.println("Gefilterde zin:\n" + palin );
System.out.println("Lengte gefilterde zin: " + palinLength + " karakters" );
System.out.println("Aantal 'woorden': \t" + woorden );
System.out.println("Aantal klinkers:\t" + klinkers );
System.out.println("Palindroom?\t" + answer );

    }

}

Upvotes: 2

Views: 1646

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

Dont compare string with ==

try this:-

if (palin.equals(nilap))

Upvotes: 1

Kon
Kon

Reputation: 10810

This line is the culprit

if (palin==nilap)

You cannot compare Strings with == in Java. You must use .equals().

if (palin.equals(nilap))

When you compare 2 objects (and Strings are objects in Java) with ==, you are comparing the references to see if they point to the same place in memory. When you use the inherited .equals() method, you are comparing the actual "meaningful values" of the objects, in this case the characters that make up the String.

This is maybe the most common mistake for newer Java programmers. See this: How do I compare strings in Java?

Upvotes: 2

Related Questions