hello can you help me i make a program that will determine palindrome words using array...how can i determine if my words are palindrome...

example if i input:
radar == radar-->this is palindrome
so if i input this
notice--->this will print not a palindrome

please help me here is my code...

String []pal=new String[1];
    boolean found=false;
    int forward;
    int backward;
    int i;
			
    System.out.println("Input palindrome");
			
			
    for( i=0;i<pal.length;i++)
	{
				
	     pal[i]=console.next();
	}
							System.out.println(Arrays.toString(pal));
			
			
	forward = 0;
	backward=pal.length-1;
			
while(forward<=backward)
      {    if(pal[i].charAt(forward)!=pal[i].charAt(backward))
		    	
     found=true;
      forward++;
      backward--;
      break;
		    	
 }
		    
if(found)
System.out.println(" palindrome");	
else
System.out.println("not palindrome");
		    	
	}
}

pal seems to mean two different things here. Review the initializations and the input portion of the program, and see if you can clear that up.

pal seems to mean two different things here. Review the initializations and the input portion of the program, and see if you can clear that up.

hello sir thank you for the reply, sir can you explain me why is it two different things.do you want me change the pal to another string variable?did i get the point sir..correct me if i am wrong..hoping for your positive responds...

Oh, pardon me. I was confused by your code, which is, we might say, unorthodox in its approach. I thought you were trying to use pal as both an array of strings and as an array of chars. My mistake.
I don't understand why pal is an array, though, and why you're using a for loop. It's one string, and you're getting it from the user one time, right?

Oh, pardon me. I was confused by your code, which is, we might say, unorthodox in its approach. I thought you were trying to use pal as both an array of strings and as an array of chars. My mistake.
I don't understand why pal is an array, though, and why you're using a for loop. It's one string, and you're getting it from the user one time, right?

sir ahm it should be done in array sir,can you help me sir...hoping for your positive responds...

String []names=new String[1];
			boolean found;
			int forward;
			int backward;
			int i;
			
			System.out.println("Input palindrome");
			
			
			for( i=0;i<names.length;i++)
			{
				
				names[i]=console.next();
			}
				
			
				
			System.out.println(Arrays.toString(names));
			
			
			forward = 0;
			backward=names.length-1;
			
			found=false;
		     while(forward<backward)
		    {
		    	if(names[i].charAt(forward)!=names[i].charAt(backward))
		    	
		    	found=true;
		    	
		    	
		    }
		        forward++;
		    	backward--; 
		    //	break;
		    	
		   if(found)
		    	System.out.println(" palindrome");	
		    else
		    	System.out.println("not palindrome");
		    	
		}
	}

Oh, pardon me. I was confused by your code, which is, we might say, unorthodox in its approach. I thought you were trying to use pal as both an array of strings and as an array of chars. My mistake.
I don't understand why pal is an array, though, and why you're using a for loop. It's one string, and you're getting it from the user one time, right?

sir ahm it should be done in array sir,can you help me sir...hoping for your positive responds...

and it should check if the word is palindrome...we are said to use array..

String []names=new String[1];
			boolean found;
			int forward;
			int backward;
			int i;
			
			System.out.println("Input palindrome");
			
			
			for( i=0;i<names.length;i++)
			{
				
				names[i]=console.next();
			}
				
			
				
			System.out.println(Arrays.toString(names));
			
			
			forward = 0;
			backward=names.length-1;
			
			found=false;
		     while(forward<backward)
		    {
		    	if(names[i].charAt(forward)!=names[i].charAt(backward))
		    	
		    	found=true;
		    	
		    	
		    }
		        forward++;
		    	backward--; 
		    //	break;
		    	
		   if(found)
		    	System.out.println(" palindrome");	
		    else
		    	System.out.println("not palindrome");
		    	
		}
	}

In line 3 you should define a String instance pal instead of String array.
Therefore you have to modified the following code related to the pal.
The instance "console" is created by the constructor Scanner, so the lines 10-18 should be replace by one line:
Scanner console = new Scanner(System.in);
line 21: backward=pal.length-1;
should be altered into
backward=pal.length()-1;
since pal is a String instance and length() is a memger method.

in Line 33 shoud be:
if(!found)

I hope you will be able to complete your assignment successfully.

An array of one member, accessed in a loop of one? I'm pretty sure that's not what your instructor was looking for.

But let it be, it's not necessarily a fatal error. What's the problem you're having? What are you asking about?

An array of one member, accessed in a loop of one? I'm pretty sure that's not what your instructor was looking for.

But let it be, it's not necessarily a fatal error. What's the problem you're having? What are you asking about?

okay sir thanks for your comments sir, my problem sir is to determine if the one i input is palindrome otherwise this will print not a palindrome..

my code sir is always print palindrome. can you help me sir hoping for your positive responds..

my example is
radar ---> this is palindrome because if we reverse the word it is still radar..

avatar--->this should print not a palindrome...

can you help me sir..thank you in advance hoping for your positive responds...

palindrome is a String instance or an array of char.
You ask the client to input one instance of String rather than an array of String.
Then you should process (work) on this instance to determine if it is polindrome or not. Therefore we are dealing with one instance of String only. We will compare the first char with the last char in the string.

palindrome is a String instance or an array of char.
You ask the client to input one instance of String rather than an array of String.
Then you should process (work) on this instance to determine if it is polindrome or not. Therefore we are dealing with one instance of String only. We will compare the first char with the last char in the string.

thank you for the reply,, yes sir can you put some comments of my code sir so that i can more understand on it..hoping for your positive responds...thank you in advance...

Tong - a string is an array of char, if you're writing C. In java, it may well be an array under there, but we have no way of knowing, and it's bad practice to assume it's anything but a class with defined methods.

Jemz - your program will print "not a palindrome" if a certain variable contains the value "false". Under what circumstances can that variable hold that value?

Jemz - you've got it the wrong way around. You're supposed to put comments in your code so others can understand it!

You have almost done in addition to the String array.
The partial corrected code could be :

String pal;
    boolean found=false;
    int forward;
    int backward;
    int i;
    Scanner console = new Scanner(System.in);			
    System.out.println("Input palindrome");
	pal=console.next();			
	forward = 0;
	backward=pal.length()-1;			
while(forward<=backward)
      {    if(pal.charAt(forward)!=pal.charAt(backward))
		    	
     found=true;
      forward++;
      backward--;
      break;		    	
      }

You may write in your own way, and complete your work.
I am looking forward to seeing your success.
Good luck

You have almost done in addition to the String array.
The partial corrected code could be :

String pal;
    boolean found=false;
    int forward;
    int backward;
    int i;
    Scanner console = new Scanner(System.in);			
    System.out.println("Input palindrome");
	pal=console.next();			
	forward = 0;
	backward=pal.length()-1;			
while(forward<=backward)
      {    if(pal.charAt(forward)!=pal.charAt(backward))
		    	
     found=true;
      forward++;
      backward--;
      break;		    	
      }

You may write in your own way, and complete your work.
I am looking forward to seeing your success.
Good luck

sir i tried it but it will always print palindrome even if it is not a palindrome
hoping for your positive responds..s

System.out.println("Input palindrome");
		    names=console.next();	
				
			System.out.println(Arrays.toString(names));
			
			
			forward = 0;
			backward=names.length()-1;
			
			found=false;
		    while(forward<backward)
		    {
		    	if(names.charAt(forward)!=names.charAt(backward))
		    	
		    	found=true;
		    	forward++;
		    	backward--; 
		    	
		    }
		        
		   
		    	
		   if(found)
		    	System.out.println(" palindrome");	
		    else
		    	System.out.println("not palindrome");
		    	
		}
	}

You have almost done in addition to the String array.
The partial corrected code could be :

String pal;
    boolean found=false;
    int forward;
    int backward;
    int i;
    Scanner console = new Scanner(System.in);			
    System.out.println("Input palindrome");
	pal=console.next();			
	forward = 0;
	backward=pal.length()-1;			
while(forward<=backward)
      {    if(pal.charAt(forward)!=pal.charAt(backward))
		    	
     found=true;
      forward++;
      backward--;
      break;		    	
      }

You may write in your own way, and complete your work.
I am looking forward to seeing your success.
Good luck

sir i tried it but it will always print palindrome even if it is not a palindrome
hoping for your positive responds...

System.out.println("Input palindrome");
		    names=console.next();	
				
			System.out.println(Arrays.toString(names));
			
			
			forward = 0;
			backward=names.length()-1;
			
			found=false;
		    while(forward<backward)
		    {
		    	if(names.charAt(forward)!=names.charAt(backward))
		    	
		    	found=true;
		    	forward++;
		    	backward--; 
		    	
		    }
		        
		   
		    	
		   if(found)
		    	System.out.println(" palindrome");	
		    else
		    	System.out.println("not palindrome");
		    	
		}
	}

Jemz - you've got it the wrong way around. You're supposed to put comments in your code so others can understand it!

sir ahm please correct my code sir
hoping for your positive responds...thank you in advance..

String names;
		    System.out.println("Input palindrome");
		    names=console.next();	
				
		
			
			
			forward = 0;
			backward=names.length()-1;
			
			found=true;
		    while(forward<backward)
		    {
		    	if(names.charAt(forward)!=names.charAt(backward))
		    	
		    	found=false;
		    	forward++;
		    	backward--; 
		    	
		    }
		        
		    
		    	
		   if(found)
		    	System.out.println(" palindrome");	
		    else
		    	System.out.println("not palindrome");
		    	
		}
	}

What's it doing that you don't like? Try the following inputs:

radar
abccba
sewer
skewer

as a few test cases.

(I've got my own stuff to work on - I'm not going to drop it to load up what you're posting and debug it for you, though if you tell me what you're having trouble with I can probably help you sort it out)

What's it doing that you don't like? Try the following inputs:

radar
abccba
sewer
skewer

as a few test cases.

(I've got my own stuff to work on - I'm not going to drop it to load up what you're posting and debug it for you, though if you tell me what you're having trouble with I can probably help you sort it out)

hello sir,


my code works sir, i just want to tell you if i did it right..hoping for your positive responds...

Wikipedia
http://en.wikipedia.org/wiki/Palindrome
says that “A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing.”. From this web site you may see different types of palindrome: characters, phrases, words, Molecular biology, and so on.
Using String instance we may determine if the string (or char array) is a palindrome as we do currently.
We may define a palindromw poet (sentence) in terms of a String array, as you initially intended.. For example:
"Step on no pets"
The following code I have modified is an example showing how to determine a palindrome. I have tested. It works. Please check the code to see if there is any error or to provide comments.

import java.util.*;
 public class PPP {
 	public static void main(String args[]) {
    String pal;
    boolean found=false;
    int forward;
    int backward;
    int i;
    Scanner console = new Scanner(System.in);	 // create an input channel		
    System.out.println("Input palindrome");
	pal=console.next();// receive a string (char array) via DOS window	
	forward = 0; // the head subscript
	backward=pal.length()-1;// the tail subscript		
while(forward<=backward){    /*when two subscripts did not meet, or just meet , i.e. did not cross */
	if(pal.charAt(forward)!=pal.charAt(backward)){ /* if the corresponding chars are not the same */
     found=true; //it is not a palindrome
     break; // jump out of the while loop
     }
      forward++;  // rightward the subscript to do the next pair of characters
      backward--;	// leftward the subscript 	to do the next pair of characters    	
      }		    
if(!found) // if it is a palindrome
System.out.println(" palindrome");	
else
System.out.println("not palindrome");
		    	
	}
}

Wikipedia
http://en.wikipedia.org/wiki/Palindrome
says that “A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing.”. From this web site you may see different types of palindrome: characters, phrases, words, Molecular biology, and so on.
Using String instance we may determine if the string (or char array) is a palindrome as we do currently.
We may define a palindromw poet (sentence) in terms of a String array, as you initially intended.. For example:
"Step on no pets"
The following code I have modified is an example showing how to determine a palindrome. I have tested. It works. Please check the code to see if there is any error or to provide comments.

import java.util.*;
 public class PPP {
 	public static void main(String args[]) {
    String pal;
    boolean found=false;
    int forward;
    int backward;
    int i;
    Scanner console = new Scanner(System.in);			
    System.out.println("Input palindrome");
	pal=console.next();			
	forward = 0;
	backward=pal.length()-1;			
while(forward<=backward){    
	if(pal.charAt(forward)!=pal.charAt(backward)){
     found=true;
     break;
     }
      forward++;
      backward--;		    	
      }		    
if(!found)
System.out.println(" palindrome");	
else
System.out.println("not palindrome");
		    	
	}
}

hello sir thank you for helping me and it works....more power to you....did i make it right doing the code is it sufficient?

I have checked your code, done same kind of "proof reading". Good work. Well done.
The final code you have made is printed as follows. Please test your final code again. In software development, TESTING is PROVING/EVALUATING

import java.util.*;
 public class P {
 	public static void main(String arg[]){
 	String names;
 	Scanner console=new Scanner(System.in); // establish a input channel
		    System.out.println("Input palindrome");
		    names=console.next();				
	int		forward = 0;
	int		backward=names.length()-1;	
	boolean	found=true;
		    while(forward<backward) {
		    	if(names.charAt(forward)!=names.charAt(backward)){
		    	found=false;
		    	break;
		    	}
		    	forward++;
		    	backward--; 		    	
		    }	    	
		   if(found)
		    	System.out.println(" palindrome");	
		    else
		    	System.out.println("not palindrome");
		    	
		}
}

I have checked your code, done same kind of "proof reading". Good work. Well done.
The final code you have made is printed as follows. Please test your final code again. In software development, TESTING is PROVING/EVALUATING

import java.util.*;
 public class P {
 	public static void main(String arg[]){
 	String names;
 	Scanner console=new Scanner(System.in); // establish a input channel
		    System.out.println("Input palindrome");
		    names=console.next();				
	int		forward = 0;
	int		backward=names.length()-1;	
	boolean	found=true;
		    while(forward<backward) {
		    	if(names.charAt(forward)!=names.charAt(backward)){
		    	found=false;
		    	break;
		    	}
		    	forward++;
		    	backward--; 		    	
		    }	    	
		   if(found)
		    	System.out.println(" palindrome");	
		    else
		    	System.out.println("not palindrome");
		    	
		}
}

sir i modify my code sir i want to have solution in array...my problem is ArrayIndexOfBoungException...please help me on this code sir and please correct my code sir if i did not make it the right way...hoping for your positive responds.

String []words = new String[5];
			
			
			int forward=0;
			int backward=words.length-1;
			int i;
			
			System.out.println("Input palindrome word");
			
			
			
			for(i=0;i<words.length;i++)
			{
				
				words[i]=console.next();
					
			}
			
			boolean found=true;
			while(forward<backward)
			{
				if(words[i].charAt(forward)!=words[i].charAt(backward))
				{
					found=false;
					break;
					
				}
				
				forward++;
				backward--;
				
				
			}
			
				if(found)
				System.out.println("palindrome");
		    	else
				System.out.println("Not Palindrome");
					
			
		}

An array of one member, accessed in a loop of one? I'm pretty sure that's not what your instructor was looking for.

But let it be, it's not necessarily a fatal error. What's the problem you're having? What are you asking about?

hello sir i modified my code and i want to do array soloution..and i have problem with my code i got error ArrayIndexOutOfBounds...please correct me sir..hoping for your positive responds...

String []words = new String[5];
			
			int forward=0;
			int backward=words.length-1;
			int i;
			
			System.out.println("Input palindrome word");
			
			
		
			for(i=0;i<words.length;i++)
			{
				
				words[i]=console.next();
					
			}
			 
			boolean found=true;
			while(forward<backward)
			{
			
				if(words[i].charAt(forward)!=words[i].charAt(backward))
				{
					found=false;
					break;
					
				}
				
				forward++;
				backward--;
				
				
			}
			
					
				if(found)
			      System.out.println("palindrome");
		    	     else
			    	System.out.println("Not Palindrome");
				
			
		}


 }

After the program has receive 5 words inputed, the i became 5. The the program goes to line 22 : if(words.charAt(forward)!=words.charAt(backward))
and the index outbounds since you have defined the the length (size) of the String array "word" is 5.

Your code is able to check a series of strings a client inputs (in which we determine palindrome in terms of series of words) instead of one string (in which we determine palindrome in terms of characters).
I have modified your code as follows:

import java.io.*;
 public class jemz {
 	
 public static void main(String args[]){
  String []words = new String[5];
  Console console = System.console();
						
			int forward=0;
			int backward=words.length-1;
			int i;
			
			System.out.println("Input 5 words");
						
			for(i=0;i<words.length;i++)
			words[i]=new String(console.readLine("Input the " + (i+1) +" word:\n"));
								
			boolean found=true;
			while(forward<backward)	{
				if (words[forward].compareTo(words[backward]) != 0)	{
					found=false;
					break;					
				}
				
				forward++;
				backward--;								
			}
				if(found)
				System.out.println("palindrome");
		    	else
				System.out.println("Not Palindrome");			
		}
}

The testing output:
Input 5 words
Input the 1 word:
jemz
Input the 2 word:
loves
Input the 3 word:
JAVA
Input the 4 word:
loves
Input the 5 word:
jemz
palindrome

After the program has receive 5 words inputed, the i became 5. The the program goes to line 22 : if(words.charAt(forward)!=words.charAt(backward))
and the index outbounds since you have defined the the length (size) of the String array "word" is 5.

Your code is able to check a series of strings a client inputs instead of one string.
I have modified your code as follows:

import java.io.*;
 public class jemz {
 	
 public static void main(String args[]){
  String []words = new String[5];
  Console console = System.console();
						
			int forward=0;
			int backward=words.length-1;
			int i;
			
			System.out.println("Input 5 words");
						
			for(i=0;i<words.length;i++)
			words[i]=new String(console.readLine("Input the " + (i+1) +" word:\n"));
								
			boolean found=true;
			while(forward<backward)	{
				if (words[forward].compareTo(words[backward]) != 0)	{
					found=false;
					break;					
				}
				
				forward++;
				backward--;								
			}
				if(found)
				System.out.println("palindrome");
		    	else
				System.out.println("Not Palindrome");			
		}
}

The testing output:
Input 5 words
Input the 1 word:
jemz
Input the 2 word:
loves
Input the 3 word:
JAVA
Input the 4 word:
loves
Input the 5 word:
jemz
palindrome

sir thank you for the reply ahm why is it will print palindrome?i think those words are not palindrome.please correct me if i am wrong sir...

It prints palindrome because the sequence "jemz loves java loves jemz" reads the same backwards and forwards, reading at the word level. That may or not be what you want, it sounds like it isn't. This is why it's good to be very specific about what it is you're trying to do.

In this case the program checks the sentence to see if its series of words are palindrome.
The sentence in terms of words:
jemz loves JAVA loves jemz
is palindrome since you see the symmetrical pattern:
jemz to jemz
loves to loves
and
the JAVA to itself JAVA.
The following sentence is not a palindrome: zemz loves JAVA loves aaaa since it is not symmetric.
This is the rule to compare the series of words to see if they are palindrome or not.
Of course, you may modify the rule, e.g. ignoring the the difference in upper/lower case , ignoring interpunction....

It prints palindrome because the sequence "jemz loves java loves jemz" reads the same backwards and forwards, reading at the word level. That may or not be what you want, it sounds like it isn't. This is why it's good to be very specific about what it is you're trying to do.

hello sir thank you for the reply,okay, i thought it will read as like this

example input
jemz
radar
loves
sos

the output:

jemz = not palindrome
radar = palindrome
loves = not palindrome
sos = palindrome


can this be possible with the same with my given example output sir?..hoping for your positive responds...

Yes. You've got the input and it's going into an array, right? So you need to
(1)loop over the input array, which you know how to do (lines 13-18 of your posted code) and
(2)check each member of the array to see that it's a palindrome (your current code only checks one of the words, the last one, but you know how to check that it's a palindrome) and
(3)put the result into an array of results, which you know how to do.

This suggests that you could have (1), (2), and (3) in the same loop, since you're doing the same thing to everything you handle, and the results should have the same index as the input.

So you see, you've got all the code. Now you just need to put it together so it does what you want. Have fun.

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.