6 Replies - 2323 Views - Last Post: 18 March 2010 - 08:21 AM Rate Topic: -----

#1 Guest_steve33*


Reputation:

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


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
	  }
	
	}



Is This A Good Question/Topic? 0

Replies To: Command line arguments

#2 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

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.

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
Was This Post Helpful? 0
  • +
  • -

#3 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

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

	    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

Was This Post Helpful? 0
  • +
  • -

#4 Guest_steve33*


Reputation:

Re: Command line arguments

Posted 17 March 2010 - 08:28 AM

View PostPaul-, 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

	    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
Was This Post Helpful? 0

#5 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

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...
Was This Post Helpful? 0
  • +
  • -

#6 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Command line arguments

Posted 18 March 2010 - 06:48 AM

View Poststeve33, 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"

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:
Attached Image
Was This Post Helpful? 1
  • +
  • -

#7 Guest_steve33*


Reputation:

Re: Command line arguments

Posted 18 March 2010 - 08:21 AM

Thank you! i finally got the program to run correctly. Thanks again
Was This Post Helpful? 0

Page 1 of 1