Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Java Palindrome Program

Azhar123Azhar123 Posts: 2Member
Hi! Just started using Java not to long ago and have a question for a program

I'm suppoosed to check to see if an entered String is a palindrome or not..I have created the checking method, but do not know if it is correct. I also have to still create the constructer method. Here is what I have so far:


// Lab16ast.java
// This is the student version of the Lab16a assignment.


import java.io.*;


public class Lab16a
{
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string ===>> ");
Palindrome p=new Palindrome(input.readLine());
p.displayData();


}
}


class Palindrome
{
private String s;
private boolean palindrome;

private boolean isPal()
{

int start = 0;
int end = s.length() - 1;
while (start <= end)
{
if (s.charAt(start) != s.charAt(end))
{
return false;
}
start++;
end--;
}

return true;

}

public Palindrome(String s)
{


}

public void displayData()
{
System.out.println(s);
System.out.println(palindrome);

}


}


Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi! Just started using Java not to long ago and have a question for a program
    :
    : I'm suppoosed to check to see if an entered String is a palindrome or not..I have created the checking method, but do not know if it is correct. I also have to still create the constructer method. Here is what I have so far:
    :
    :
    : // Lab16ast.java
    : // This is the student version of the Lab16a assignment.
    :
    :
    : import java.io.*;
    :
    :
    : public class Lab16a
    : {
    : public static void main (String args[]) throws IOException
    : {
    : BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    : System.out.println("Enter a string ===>> ");
    : Palindrome p=new Palindrome(input.readLine());
    : p.displayData();
    :
    :
    : }
    : }
    :
    :
    : class Palindrome
    : {
    : private String s;
    : private boolean palindrome;
    :
    : private boolean isPal()
    : {
    :
    : int start = 0;
    : int end = s.length() - 1;
    : while (start <= end)
    : {
    : if (s.charAt(start) != s.charAt(end))
    : {
    : return false;
    : }
    : start++;
    : end--;
    : }
    :
    : return true;
    :
    : }
    :
    : public Palindrome(String s)
    : {
    :
    :
    : }
    :
    : public void displayData()
    : {
    : System.out.println(s);
    : System.out.println(palindrome);
    :
    : }
    :
    :
    : }
    :
    :
    :
    Although this is not really the right board for Java (Java and Javascript are two completely different languages) and I am not a Java programmer, I might see a problem in the isPal() method. The return value might always be true, if Java does not break out the method upon setting the return value to false.
  • chiProgrammerchiProgrammer Posts: 14Member
    Hey try this
    [code]
    import java.io.*;
    public class Palindrome
    {
    private String palin;
    public Palindrome()
    {
    palin = "";
    }
    public Palindrome(String str)
    {
    palin = str;
    }
    public static boolean testPalindrome(String s)
    {
    String a = s.toLowerCase( );
    String b = "";
    for (int x = 0; x < a.length ( ); x++)
    {
    char c = a.charAt(x);
    if (c >='a' && c <= 'z')
    {
    b += c;
    }
    }
    String d = "";
    for (int y = b.length()-1; y >= 0; y--)
    {
    d += b.charAt(y);
    }
    return (b.equals(d));
    }

    public static void main(String []args)
    throws IOException
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(" Welcome to the Palindrome Tester!!!
    Enter a word Phrase or String
    Press q or type quit to stop the program at anytime");
    String ing = "";
    do{
    System.out.println("Input?");
    ing = in.readLine();
    if (testPalindrome(ing) == true)
    {
    System.out.println(" "+ing+" is a palindrome");
    }
    else
    {
    System.out.println(" "+ing+" is not a palindrome");
    }
    }while (!ing.equalsIgnoreCase("q") &&
    !ing.equalsIgnoreCase("quit"));

    }
    }
    [/code]

    : : Hi! Just started using Java not to long ago and have a question for a program
    : :
    : : I'm suppoosed to check to see if an entered String is a palindrome or not..I have created the checking method, but do not know if it is correct. I also have to still create the constructer method. Here is what I have so far:
    : :
    : :
    : : // Lab16ast.java
    : : // This is the student version of the Lab16a assignment.
    : :
    : :
    : : import java.io.*;
    : :
    : :
    : : public class Lab16a
    : : {
    : : public static void main (String args[]) throws IOException
    : : {
    : : BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    : : System.out.println("Enter a string ===>> ");
    : : Palindrome p=new Palindrome(input.readLine());
    : : p.displayData();
    : :
    : :
    : : }
    : : }
    : :
    : :
    : : class Palindrome
    : : {
    : : private String s;
    : : private boolean palindrome;
    : :
    : : private boolean isPal()
    : : {
    : :
    : : int start = 0;
    : : int end = s.length() - 1;
    : : while (start <= end)
    : : {
    : : if (s.charAt(start) != s.charAt(end))
    : : {
    : : return false;
    : : }
    : : start++;
    : : end--;
    : : }
    : :
    : : return true;
    : :
    : : }
    : :
    : : public Palindrome(String s)
    : : {
    : :
    : :
    : : }
    : :
    : : public void displayData()
    : : {
    : : System.out.println(s);
    : : System.out.println(palindrome);
    : :
    : : }
    : :
    : :
    : : }
    : :
    : :
    : :
    : Although this is not really the right board for Java (Java and Javascript are two completely different languages) and I am not a Java programmer, I might see a problem in the isPal() method. The return value might always be true, if Java does not break out the method upon setting the return value to false.
    :

Sign In or Register to comment.