program does not display output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kreators
    New Member
    • Nov 2009
    • 1

    program does not display output

    Hi everyone i'm newbie here in bytes and a newbie in programing i need your help! can you correct my program when my program compile its say Process completed. but when i put a Word and Run my program does not show an output what is the problem?

    Code:
    import javax.swing.*;
    
    public class Palindrome
    {
        public static void main(String[] args) 
        {
            String A;
            int i,palindrome=1;
            A=JOptionPane.showInputDialog(null,"Enter a Word");
            
            for(i=0;i<A.length()/2;++i)
            {
            	if(A.charAt(i)!=A.charAt(A.length())-i-1)
            	{
            		palindrome=0;break;
            	}
            	if(palindrome==1)
            	System.out.println("The Word "+A+" is a Palindrome"); 
            	else
            	System.out.println("The Word "+A+" is not a Palindrome");
            }
        }
    }
    Last edited by Frinavale; Dec 2 '09, 09:20 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Do you get any runtime error message? These are very helpful so, if you do, post the entire message (including the stack trace which shows where the error occurred) and also indicate which line of your code it is referring to.

    Comment

    • mrjohn
      New Member
      • May 2009
      • 31

      #3
      Two bugs.

      On line 15 (Your if-statement where you check to see if characters are the same), you have A.charAt(A.leng th())-i-1). You need to move the -i-1 part into the charAt() method, or you'll have a null pointer exception. So it'd be A.charAt(A.leng th()-i-1)).

      The other bug is that you've got your if statement that determines which output to print inside your for-loop. Since your loop exits if it finds a mismatch, it'll skip this code. To fix it, just move the if-statement out of the for-loop like so:
      [code="java"]for(i=0;i<A.len gth()/2;++i)
      {
      if(A.charAt(i)! =A.charAt(A.len gth()-i-1))//Error1
      {
      palindrome=0;br eak;
      }
      }
      if(palindrome== 1)
      System.out.prin tln("The Word "+A+" is a Palindrome");
      else
      System.out.prin tln("The Word "+A+" is not a Palindrome");[/code]

      Comment

      Working...