• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checking for Palindrome using Arrays

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I am working on a class project where I need to check and see if a string of characters entered by a user is a palindrome or not. I have made the following code so far, but when I execute the code it says EVERYTHING is a palindrome, even completely random things that are not a palindrome lol.

NOTE: I have searched this forum for help from others that were given this problem, but I do not want to use their code, I would like to use my own code and just get help.

EDIT: I have also made a github incase you find it easier to look through that way. https://gist.github.com/anonymous/cf1d66899190e10fcbe6

Here is my main code:



Here is the Class code:



 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Debug your code with System.out.println() statements.

Example, this should show you what is wrong pretty quickly:

 
Brandon Oconnor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm ok so it tells me this in the output:

Enter a string characters, terminated by a period.
hello
i=0
j=-1
number of loops executed: 0
hello is a palindrome.
Continue or Quit?
quit


Which tells me that it never executes any loops, correct?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right because "used" is always equals to 0.
 
Brandon Oconnor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm still pretty new to Java, do I need to change used and use a different variable, or do I just have to change the 0?

In the class code I have the variable incrementing and decreasing with i++ and j--
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well what about if you set "used" to the length of your word-1 or to the length of your word if you remove the requirement to enter a period after the word?

You may also have a look at java.lang.String.trim() too to further reduce the risk of errors:

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#trim--

 
Brandon Oconnor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm I've never seen or heard of that before. Any chance you could provide a small example so I can look into my code again and see how I implement it into that?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have added one more debugging statement that should further help you out. Your code is almost working...

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO, the very first code you posted would work fine if you just replace one line (the first line below) although I haven't tested it:



Of course, you could refactor to make it more elegant but it should work like this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic