Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Rewrite the CheckingPalindrome program by passing the string as a comm Posted by ryclegman on 8 Mar 2010 at 7:55 AM
Rewrite the CheckingPalindrome program by passing the string as a command-line argument. Please help because i quite frankly dont understand... What parts do i have to change and where? thanks a ton guys!
import javax.swing.JOptionPane;

public class CheckPalindrome {
  /** 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.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 s) {
    // The index of the first character in the string
    int low = 0;

    // The index of the last character in the string
    int high = s.length() - 1;

    while (low < high) {
      if (s.charAt(low) != s.charAt(high))
        return false; // Not a palindrome

      low++;
      high--;
    }

    return true; // The string is a palindrome
  }
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.