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
Joined: May 14, 2003
Posts: 758
posted
0
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-
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
Joined: Nov 24, 2001
Posts: 45
posted
0
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
Joined: Nov 24, 2001
Posts: 45
posted
0
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
Joined: Nov 26, 2003
Posts: 382
posted
0
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?
Ever Existing, Ever Conscious, Ever-new Bliss
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
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.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rama Krishna Yalamanchili
Greenhorn
Joined: Feb 11, 2004
Posts: 25
posted
0
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.....
You are not fed, if you dont ask. <img src="wink.gif" border="0"> <p>SCJP 1.4, i2CP, IBM s390 CP
Stacey Johnson
Ranch Hand
Joined: Jan 11, 2004
Posts: 55
posted
0
I would just like to thank everyone for thier help. I did get things up and running smoothly. Stacey