Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing the string as a command-line argument?

807569Sep 27 2006 — edited Sep 27 2006
Hey guys whats going on? My first post here just got a quick question hopefully.

I need to pass the string as a command line argument in this Palindrome program, how exactly do i do do that?

import javax.swing.JOptionPane;

public class PalindromeIgnoreNonAlphanumeric {
/** Main method */
public static void main(String[] args) {
// Prompt the user to enter a string
String s = JOptionPane.showInputDialog(null,
"Enter a string:", "Example 7.3 Input",
JOptionPane.QUESTION_MESSAGE);

// Declare and initialize output string
String output = "Ignoring non-alphanumeric characters, \nis "
+ s + " a palindrome? " + isPalindrome(s);

// Display the result
JOptionPane.showMessageDialog(null, output,
"Example 7.3 Output", JOptionPane.INFORMATION_MESSAGE);
}

/** Return true if a string is a palindrome */
public static boolean isPalindrome(String s) {
// Create a new string by eliminating non-alphanumeric chars
String s1 = filter(s);

// Create a new string that is the reversal of s1
String s2 = reverse(s1);

// Compare if the reversal is the same as the original string
return s2.equals(s1);
}

/** Create a new string by eliminating non-alphanumeric chars */
public static String filter(String s) {
// Create a string buffer
StringBuffer strBuf = new StringBuffer();

// Examine each char in the string to skip alphanumeric char
for (int i = 0; i < s.length(); i++) {
if (Character.isLetterOrDigit(s.charAt(i))) {
strBuf.append(s.charAt(i));
}
}

// Return a new filtered string
return strBuf.toString();
}

/** Create a new string by reversing a specified string */
public static String reverse(String s) {
StringBuffer strBuf = new StringBuffer(s);
strBuf.reverse(); // Use the reverse method for StringBuffer object
return strBuf.toString();
}
}


Thanks guys appreciate any help
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 25 2006
Added on Sep 27 2006
12 comments
541 views