import javax.swing.JOptionPane;
public class palindrome {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Prompt the user to enter a string
String s = JOptionPane.showInputDialog(null,
"Enter a string:", "Example 7.1 Input",
JOptionPane.QUESTION_MESSAGE);
// Declare and initialize output string
String output = "";
if (isPalindrome(s))
output += s + " is a palindrome";
else
output += s + " is not a palindrome";
// Display the result
JOptionPane.showMessageDialog(null, output,
"Example 7.1 Output", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
/** Check if a string is a palindrome */
public static boolean isPalindrome(String args) {
// The index of the first character in the string
int low = 0;
// The index of the last character in the string
int high = args.length() - 1;
while (low < high) {
if (args.charAt(low) != args.charAt(high))
return false; // Not a palindrome
low++;
high--;
}
return true; // The string is a palindrome
}
}
Command line arguments
Page 1 of 16 Replies - 1719 Views - Last Post: 18 March 2010 - 08:21 AM
#1 Guest_steve33*
Command line arguments
Posted 16 March 2010 - 08:31 AM
I need to change this program so that it uses command line arguments but i do not know how to use them. Im pretty sure i need some kind of array? but am not sure what goes in it or where it goes. please help me with my problem. thanks
Replies To: Command line arguments
#2
Re: Command line arguments
Posted 16 March 2010 - 08:44 AM
Actually, Java programs have command lines set up and most programmers don't even know it, like you.
The args array hold all the command line arguments.
If you call this program like this:
java palindrome happy racecar race car
Then your arguments look like this:
args[0] = happy
args[1] = racecar
args[2] = race
args[3] = car
Here is a further tutorial if you still need help with them: http://java.sun.com/...mdLineArgs.html
public static void main(String[] args)
The args array hold all the command line arguments.
If you call this program like this:
java palindrome happy racecar race car
Then your arguments look like this:
args[0] = happy
args[1] = racecar
args[2] = race
args[3] = car
Here is a further tutorial if you still need help with them: http://java.sun.com/...mdLineArgs.html
#3
Re: Command line arguments
Posted 16 March 2010 - 08:45 AM
Command-line arguments are passed in the args array (main method argument). The first argument would be args[0]. You can replace
with
Edit: I see Dogstopper beat me to the reply. One note: if your input string contains spaces, then it will be broken up into words, as in Dogstopper's example. To avoid that, enclose it in quotation marks.
If the command line is
java palindrome "happy racecar race car"
your argument array will be
args[0] = happy racecar race car
String s = JOptionPane.showInputDialog(null, "Enter a string:", "Example 7.1 Input", JOptionPane.QUESTION_MESSAGE);
with
String s = args[0];
Edit: I see Dogstopper beat me to the reply. One note: if your input string contains spaces, then it will be broken up into words, as in Dogstopper's example. To avoid that, enclose it in quotation marks.
If the command line is
java palindrome "happy racecar race car"
your argument array will be
args[0] = happy racecar race car
This post has been edited by Paul-: 16 March 2010 - 08:53 AM
#4 Guest_steve33*
Re: Command line arguments
Posted 17 March 2010 - 08:28 AM
Paul-, on 16 March 2010 - 07:45 AM, said:
Command-line arguments are passed in the args array (main method argument). The first argument would be args[0]. You can replace
with
Edit: I see Dogstopper beat me to the reply. One note: if your input string contains spaces, then it will be broken up into words, as in Dogstopper's example. To avoid that, enclose it in quotation marks.
If the command line is
java palindrome "happy racecar race car"
your argument array will be
args[0] = happy racecar race car
String s = JOptionPane.showInputDialog(null, "Enter a string:", "Example 7.1 Input", JOptionPane.QUESTION_MESSAGE);
with
String s = args[0];
Edit: I see Dogstopper beat me to the reply. One note: if your input string contains spaces, then it will be broken up into words, as in Dogstopper's example. To avoid that, enclose it in quotation marks.
If the command line is
java palindrome "happy racecar race car"
your argument array will be
args[0] = happy racecar race car
Thank you, i changed my code for what you told me to do but i have no way of entering a string to determine it is a palindrome. What do i do with the
java palindrome "happy racecar race car"
What else do i need to do to fix this problem? thanks
#5
Re: Command line arguments
Posted 17 March 2010 - 08:49 AM
Using that command line argument, all you have to do is send arg[0] through your palindrome method...
#6
Re: Command line arguments
Posted 18 March 2010 - 06:48 AM
steve33, on 17 March 2010 - 07:28 AM, said:
Thank you, i changed my code for what you told me to do but i have no way of entering a string to determine it is a palindrome. What do i do with the
java palindrome "happy racecar race car"
java palindrome "happy racecar race car"
Hm, you must be using an IDE. How do you run your program? The above command works if you run your program from a console. (In a console you type in commands, one per line. Each command can have arguments, hence the terminology.) If you are using an IDE, you have to find the place in the GUI, where you can add in your command-line arguments. For Eclipse it looks like this:
#7 Guest_steve33*
Re: Command line arguments
Posted 18 March 2010 - 08:21 AM
Thank you! i finally got the program to run correctly. Thanks again
Page 1 of 1
|
|

New Topic/Question
Reply
MultiQuote










|