Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help

 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Again,
I don't seem to be getting a much response on my other posting so I thought I'd take another go at it since I really am lost.
I'm working with command line arguments. I'm trying to have the user enter a word in the command line and the program tell the user if it is a palindrome or not. (Reminder: Palindrome is a word that is spelt the same forward and reverse ie) noon, mom, bob etc.) My text gives me the basics but I'm having problems with my "isPalindrome() method. Doing the whole command line argument is throwing me off. I"m not sure what my proper argument should be or even if using "arg[0] is correct. Can someone please give me some insite on this.
Thanks
/**import JOptionPane (extention package) to support swing*/
import javax.swing.JOptionPane
public class Exercise7_3 {
/** Main method */
public static void main(String[] args) {

//Check command-line argument
if (args.length !=1) {

JOptionPane.showMessageDialog(null, "You did not enter a string",
"Exercise 7.3, TME 2", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}

int result = 0;

//Determine

if (isPalindrome(args [0]))
System.out.print(args[0] + "is a palindrome");
else
System.out.print(args[0] + "is not a palindrome");

}
/** Check if a string is a palindrome */
public static boolean isPalindrome(args [0]) {
// The index of the first character in the string
int low = 0;
// The index of the last character in the string
int high = .length() - 1;
while (low < high) {
if (args [0].charAt(low) != args [0].charAt(high))
return false; // Not a palindrome

low++;
high--;
}
return true; // The string is a palindrome


}
}
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify, the args variable is an array of strings.
The first string in the array is arg[0], the second string (if there was one) would be arg[1], and so on.
I see at least two problems:
1. You want the method isPalindrome to be able to accept a parameter of type string. The method signature might look like:
public static boolean isPalindrome(String word)
2. Within the isPalindrome method you want to refer to the formal parameter name word where you are currently referring to args[0].
-Jeff-
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is your method signature
public static boolean isPalindrome(args [0])
{
}
have a look at this to try to see the fix
 
Nischal Topno
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Jeff Langr's response check on the code:

you should specify word.length() (or whatevername.length() that you give in your sPalindrome() method) and not just .lenght().
 
Nischal Topno
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same question is posted in two headings (Command Line Arguments, Please Help )... me getting confused where to reply and where actually the author is looking for answer and so where to reply.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moderators, is it OK (acceptable policy) for a non-moderator (like me) to point out that this a a double-post and ask the poster not to double-post or will I be ruffling some mod-feathers by doing that?
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sadanand Murthy:
Moderators, is it OK (acceptable policy) for a non-moderator (like me) to point out that this a a double-post and ask the poster not to double-post or will I be ruffling some mod-feathers by doing that?

Thank you, Sadanand Murthy. It is quite acceptable. It would be better to add a post to the original thread thus bumping it to the top rather than to start a new thread. I'll close the old one for now.
 
Rama Krishna Yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.JOptionPane;
public class Exercise7_3 {
public static void main(String[] args) {
if (args.length != 1) {
JOptionPane.showMessageDialog(null, "You did not enter a string", "Exercise 7.3, TME 2", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
int result = 0;
if (isPalindrome(args[0])) {
System.out.print(args[0] + " is a palindrome ");
}
else {
System.out.print(args[0] + " is not a palindrome ");
}
}
public static boolean isPalindrome(String abcd) {
int low = 0;
int high = abcd.length() - 1;
while (low < high) {
if (abcd.charAt(low) != abcd.charAt(high))
return false; // Not a palindrome
low++;
high--;
}
return true; // The string is a palindrome
}
}

This is the code to compile.....
 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just like to thank everyone for thier help. I did get things up and running smoothly.
Stacey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic