Does anyone have an example or a starting point for this? I have
written a program that checks for palindromes but I am lost with this.
Any help would be great, thank you!


Create a class that models a palindrome. The program should have getter and setter methods for the instance variable storing the palindrome. A helper method should indicate whether a word is or is not a palindrome. The setter method should only set the palindrome instance variable if the proposed word is in fact a palindrome. Use the main method to instantiate a palindrome class and verify that its getter and setter methods work correctly.

It appears that you're having problems creating a class hey? So I'm going to give you and example and clear up some of the termonolgy. Consider:

class A
{
    private String name;
    public A()
    {
        name = "Test";
    }
    public void setName(String n)
    {
        name = n;
    }
    public String getName()
    {
        return name;
    }
    public void process()
    {
        System.out.println(name);
    }
}

So here our class is A. It has one instance variable, viz name. name is an instance variable because for each object your create of class A, that object has access to it's own name (i.e. it has its own instance of name).
So the methods getName() and setName() are used to gain access to the instance variable. This would be similar to your setter() and getter() methods. process() would be the method that actually uses the instance variable, just like your helper() method.
Does this clear up anything? Or perhaps I've went totally off topic?
Peace!

Hi! I hope that my solution for your problem will help you.Write me back if you are satisfate with the code.:lol:

Regards,
Andrijana Denkovic
[EMAIL="andrijana_denkovic@yahoo.co.uk"]andrijana_denkovic@yahoo.co.uk[/EMAIL]

-----------------------------------------------------------------------------

package scrPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Palindrom {
    String pal;
    // constructor 

    public Palindrom(){  
        this.pal="";
    }

    public Palindrom(String str){  
        this.pal=str;
    }

    public boolean helper(){
        int len=this.pal.length();
        for(int i =0;i<(len % 2);i++ ){
            if (this.pal.charAt(i)!=this.pal.charAt(len-i-1)){
                return false;
            }
        }

        return true;
    }

    public void setter(String pal) {
        this.pal = pal;
    }

    public String getter() {
        return pal;
    }

    // MAIN METHOD
    public static void main(String[] args) throws IOException{
        //instantiate Plaindrom object 
        Palindrom palObj=new Palindrom();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            do  {
                System.out.print("Enter some word(Press '/' for end): ");
                str = in.readLine();
                if (0==str.compareTo("/")){
                    break;        
                }else
                {
                    palObj.setter(str);
                    if (palObj.helper()){
                        System.out.println("\n"+palObj.getter()+" is Plaindrom!");
                    }
                    else{
                        System.out.println("\n"+palObj.getter()+" is not Plaindrom!");
                    }
                }
            }while (true);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }//main

}

-----------------------------------------------------------------------------

Thank you guys for taking the time to help me out, I really appreciate it!

I hope this is easiest code to find palindrome of length five & help you a lot.

import java.util.Scanner;

public class Palindrom {
    
    
   public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String s="";
        
        
            System.out.println("Enter no of length 5");
            if(sc.hasNext())
                s =s+sc.next();
            if(s.length()!=5)
               System.out.println("Wrong Number Enter again");
            
            if(s.charAt(0)==s.charAt(4) && s.charAt(1)==s.charAt(3))
                System.out.println(""+s+" Is Palindrom");
            else
                System.out.println(s+" Is not Palindrom");
            
    }    
}

To sumitlole.

NO, it is not the easiest way, it is the wrong way. This thread is 2.5 years old. What do you think you can accomplish.

In your code you have this:

System.out.println("Wrong Number Enter again");

if(s.charAt(0)==s.charAt(4) && s.charAt(1)==s.charAt(3))

You print the message "try again" but then you don't have a while loop for the user to enter again a value until the right one is entered. No matter the size of the input this code will be executed: if(s.charAt(0)==s.charAt(4) && s.charAt(1)==s.charAt(3)) which result to your program to crash.
Not to mention other little mistakes that make your code not safe to run

About your edit:
Use code tags !! Your code is difficult to read with all these colors

If all u need is a code snippet for checking whether a given word is a palindrome, then use the one below:

import java.io.*;
import java.lang.*;
/**
 *
 * @author Developer
 */
public class Palindrome
{
   
public static void main(String[] args)throws IOException
{
Console console=System.console();
String word=console.readLine("Enter a word of yr choice");
int flag=1;
int left=0;
int right=word.length()-1;
while(left<right)
{
if(word.charAt(left)!=word.charAt(right))
{
    flag=0;
}
left++;
right--;
if(flag==1)
{
System.out.println("The word" + word + "is a palindrome");
}
else
{
System.out.println("The word"+ word + "is not a palindrome");
}

}

}
}

If all u need is a code snippet for checking whether a given word is a palindrome, then use the one below:

I found interesting that code but noticed a few things that I didn't like:

while(left<right)
{
  if(word.charAt(left)!=word.charAt(right))
  {
     flag=0;
  }
  left++;
  right--;

  [B]if(flag==1)
  {
  System.out.println("The word" + word + "is a palindrome");
  }
  else
  {
  System.out.println("The word"+ word + "is not a palindrome");
  }[/B]
}

The bolded part shouldn't be in the loop.
Take this input: "abcdefgcba"
Because for the first checks it will print that it is palindrome, then it will keep printing that is not for a few loops before exiting.
You should print the result outside the loop, only once.
Also when the flag is set to 0 you can break from the loop because it is not necessary to continue checking. You found that it is not a palindrome so no need to check the rest of the word

Class Palindrome
{
public static void main(String args[])
{
String s="malayalam";
int i;
int n=s.length();
String str="";
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);
if(str.equals(s))
System.out.println(s+ "is palindrome");
else
System.out.println(s+ "is not a palindrome");
}
}

import java.io.*;
import javax.swing.JOptionPane;
 public class PalindromeCouplet{   


 static boolean palindromeCouplet (String s)   {
 	StringBuffer s1 = new StringBuffer(s);
 return ((s.compareTo(new String(s1.reverse()))==0) ? true :false)  ;
 	}	

 public static void main(String[] args) {	
 	while(true){
    String str=JOptionPane.showInputDialog( null,  
	"Type in a string!\nPress Cancel to terminate the program",
	"Palindrome or Not?",
	JOptionPane.QUESTION_MESSAGE);    
    
	if (str == null) {
    	System.out.println("Bye for now!");
    	break;
    	} 

System.out.println("Palindrome? " + palindromeCouplet(str));
          }
      }   
}

Create an application that reads in a five-digit integers and determines whether it is a palindrome.If the number is not five digits long,display an error messsage and allow the user to enter a new value.

Create an application that reads in a five-digit integers and determines whether it is a palindrome.If the number is not five digits long,display an error messsage and allow the user to enter a new value.

Read the forum's rules. Also, read whatever has been written in this thread. Since you found it, you should know that it answers your question.

a java application that asks an INTEGER number from the user.Then output each digit in a newline.

Member Avatar for coil

1. Please don't revive old topics. Start a new one if you still have questions.
2. This is not a code-writing service. We can help you, but you need to show some effort.
3. Integers and Strings are not that different for palindromes - you can even convert the int to a String if that helps. All you need is in this thread.

a java application that asks an INTEGER number from the user.Then output each digit in a newline.

Haven't you read my previous reply? Your initial question has been answered in this thread. For new questions start an new thread. What do you think this is? A thread were you can throw your homework and disregard the forum rules? If you want help start a new thread and read the forum rules, which you agreed when you signed in.

The more simplest one is

public class Palindrome {
	static public String pal(String str, int i, int j){
		if(str.length()/2 != i)
			if(str.charAt(i)==str.charAt(j))
				pal(str,++i,--j);
			else
				return	"String is Not palaindrome";
		
		return "String is palaindrome";
	}
	public static void main(String[] args) {
		String str = args[0];
		System.out.println(pal(str,0,str.length()-1));
	}
}

The more simplest one is

public class Palindrome {
	static public String pal(String str, int i, int j){
		if(str.length()/2 != i)
			if(str.charAt(i)==str.charAt(j))
				pal(str,++i,--j);
			else
				return	"String is Not palaindrome";
		
		return "String is palaindrome";
	}
	public static void main(String[] args) {
		String str = args[0];
		System.out.println(pal(str,0,str.length()-1));
	}
}

an even easier way would have been to read the dates of the last posts, realise it is an ancient, dead thread and either already answered, or the OP is no longer following this thread.

this thread may not yet have been marked as 'closed' but if you bothered to look at the dates of the posts, you should have realised it should be treated as if it were.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.