Help with command line arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LyndsayJ
    New Member
    • Apr 2007
    • 2

    #1

    Help with command line arguments

    Hi, I am working on a java program for my class where I have to rewrite a code that checks whether the input is a palindrom by passing the string as a command-line argument.
    Here is the code that I have to rewrite. PLEASE HELP!!
    Code:
    import javax.swing.JOptionPane;
    
    public class CheckPalindrome
    {
    	public static void main(String [] args)
    	{
    		String s = JOptionPane.showInputDialog("Enter a string");
    		
    		String output = "";
    		
    		if(isPalindrome(s))
    			output = s + " is a palindrome.";
    		else
    			output = s + " is not a palindrome.";
    		
    		JOptionPane.showMessageDialog(null, output);
    	}
    	
    	public static boolean isPalindrome(String s)
    	{
    		int low = 0;
    		
    		int high = s.length() - 1;
    		
    		while(low < high)
    		{
    			if(s.charAt(low) != s,charAt(high))
    				return false;
    				
    			low++;
    			high--;
    		}
    		return true;
    	}
    }
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    welcome to TSDN

    u r trying to compare the first half characters and the last half characters.
    is this right to check a string .... palyndrom????

    what J2SE version u r using????
    there r lot of APIs ..... handling with string.

    if u r using Swing then why r u having main function?????

    and one more thing .... ur Q. title is related to command line arguments, but u have not used this.......

    so at the end i totally mess up.....would u be more clear???

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by LyndsayJ
      Hi, I am working on a java program for my class where I have to rewrite a code that checks whether the input is a palindrom by passing the string as a command-line argument.
      Here is the code that I have to rewrite. PLEASE HELP!!
      Code:
      import javax.swing.JOptionPane;
       
      public class CheckPalindrome
      {
      	public static void main(String [] args)
      	{
      		String s = JOptionPane.showInputDialog("Enter a string");
       
      		String output = "";
       
      		if(isPalindrome(s))
      			output = s + " is a palindrome.";
      		else
      			output = s + " is not a palindrome.";
       
      		JOptionPane.showMessageDialog(null, output);
      	}
       
      	public static boolean isPalindrome(String s)
      	{
      		int low = 0;
       
      		int high = s.length() - 1;
       
      		while(low < high)
      		{
      			if(s.charAt(low) != s,charAt(high))
      				return false;
       
      			low++;
      			high--;
      		}
      		return true;
      	}
      }
      Could you tell us what specific problem you have with this.

      Comment

      • LyndsayJ
        New Member
        • Apr 2007
        • 2

        #4
        Originally posted by r035198x
        Could you tell us what specific problem you have with this.
        I thought I was specific...I have to rewrite the code that I posted in my original message so that I pass the input as a command line argument...rath er than entering the date using input dialog boxes...BUT, I figured it out...that's for responding though!

        Comment

        • cnorthcutt
          New Member
          • Oct 2007
          • 1

          #5
          I have the same problem also. What did you come up with?

          Originally posted by LyndsayJ
          Hi, I am working on a java program for my class where I have to rewrite a code that checks whether the input is a palindrom by passing the string as a command-line argument.
          Here is the code that I have to rewrite. PLEASE HELP!!
          Code:
          import javax.swing.JOptionPane;
          
          public class CheckPalindrome
          {
          	public static void main(String [] args)
          	{
          		String s = JOptionPane.showInputDialog("Enter a string");
          		
          		String output = "";
          		
          		if(isPalindrome(s))
          			output = s + " is a palindrome.";
          		else
          			output = s + " is not a palindrome.";
          		
          		JOptionPane.showMessageDialog(null, output);
          	}
          	
          	public static boolean isPalindrome(String s)
          	{
          		int low = 0;
          		
          		int high = s.length() - 1;
          		
          		while(low < high)
          		{
          			if(s.charAt(low) != s,charAt(high))
          				return false;
          				
          			low++;
          			high--;
          		}
          		return true;
          	}
          }

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by LyndsayJ
            Hi, I am working on a java program for my class where I have to rewrite a code that checks whether the input is a palindrom by passing the string as a command-line argument.
            Here is the code that I have to rewrite. PLEASE HELP!!
            Code:
            import javax.swing.JOptionPane;
            
            public class CheckPalindrome
            {
            	public static void main(String [] args)
            	{
            		String s = JOptionPane.showInputDialog("Enter a string");
            		
            		String output = "";
            		
            		if(isPalindrome(s))
            			output = s + " is a palindrome.";
            		else
            			output = s + " is not a palindrome.";
            		
            		JOptionPane.showMessageDialog(null, output);
            	}
            	
            	public static boolean isPalindrome(String s)
            	{
            		int low = 0;
            		
            		int high = s.length() - 1;
            		
            		while(low < high)
            		{
            			if(s.charAt(low) != s,charAt(high))
            				return false;
            				
            			low++;
            			high--;
            		}
            		return true;
            	}
            }
            So you don't want to use Swing :-)
            So simply do it .............

            [code=java]
            //String s = JOptionPane.sho wInputDialog("E nter a string");
            //now change this line to............. .
            String s = args[0];
            [/code]

            If you want to check whether user passes the arguments then you can also do it..

            [code=java]
            if(args.length= =0) return;
            [/code]

            Enjoy the code.

            Debasis Jana

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by cnorthcutt
              I have the same problem also. What did you come up with?
              Why don't you post what you've done so far?

              Comment

              Working...