• 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

Where did I go wrong?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I'm close but somethings wrong. Could someone help me out? Here's the code. I'm pretty sure that it's close.

import java.util.*;

public class CheckPalindrome
{
public static void main(String args[])
{
String input;
System.out.println("Hello and welcome to the palindrome program!");
System.out.println();
System.out.println("Enter quit to end the program.");

do
{
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter a sentence and I will check it:");
phrase=keyboard.next();//recording the string
palindrome(input);//checking
}
while (!input.equals("quit"));
}
String cleanSentence = clean(args[0].toCharArray());

if (isPalindrome(new String(cleanSentence))) {
System.out.println("Is a palindrome");
} else {
System.out.println("Is NOT a palindrome");


//
// returns true if word is a palindrome
//
private static boolean isPalindrome(String phrase) {
return phrase.equalsIgnoreCase((new StringBuffer(phrase)).reverse().toString().toUpperCase());

}
//
// removes all non letters from the sentence
//
private static String clean(char [] palindrome) {
String sentence = "";
for (int i = 0; i < palindrome.length; i++) {
if (Character.isLetter(palindrome[i])) {
sentence += palindrome[i];
}
}
return sentence;
}
}

Thanks in advance.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason,

What specifically is your question? what is the code doing/not doing that you expect it to?

People here love to help, but you have to give us a little guidence as to how to help you.

Also, please learn to use the code tags. it is MUCH easier to read your code if you surround your code with the proper tags as it will preserve indentation. There are a bunch of buttons below where you enter your posts... click the one called "code" and you'll see the tags appear at the end of what you've typed. just paste your formatted code between the tags, and life will be much easier for everyone...

thanks!!!
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason,

First up, when posting code, please use the UBB Code function so that your code shows as formatted code - you know indents and the like.

As far as what you have posted is concerned, the reference variable "phrase" has not been declared with a type (I assume it would be of type String). You also make a reference to a method called "palindrome(....)" and this method is no where to be found.

Regards,
JD
[ March 18, 2005: Message edited by: John Dell'Oso ]
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also think you have code outside of a method... you have a do-while loop in your main..

after the " while (!input.equals("quit"));" line, you have a closing brace. i think this closes your main() method, but then you have more code. you can't do this.

i only say i'm not sure because without indenting, it's hard to read. plus, it's early, and i make bad assumptions about code before 9am.
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that! This was the first time I ever posted code to the board so I wasn't sure how to go about it. (Copying and pasting has got me in trouble before and it probably will again! ) I'll do better next time, promise!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic