Results 1 to 9 of 9

Thread: Palindrome

  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question Palindrome

    guyz , ive done an pplet that gets the number n shows the reverse form of it . im gonna add a function that decides if the reversed number imy pSs palindrome or not ... do u have any idea ?
    here's my program :

    Java Code:
    /**
     * @(#)actionListener.java
     *
     * actionListener Applet application
     *
     * @author 
     * @version 1.00 2010/4/21
     */
     
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class reverse extends Applet implements ActionListener {
    
    	private Label enterno1,result1;
    	private TextField enterno2,result2;
    	private Button reverse,clear;
    		
    	public void init() {
    		
    	enterno1=new Label(" Enter your number : ");
    	result1=new Label(" Reverse Form");
    	reverse=new Button("REVERSE ! ");
    	clear=new Button("CLEAR");
    	enterno2=new TextField(10);
    	result2=new TextField(10);
    	
    	add(enterno1);
    	add(enterno2);
    	
    	add(result1);
    	add(result2);
    	
    	add(reverse);
    	add(clear);
    	
    	
    	reverse.addActionListener(this);
    		clear.addActionListener(this);
    		
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		String rev="";
    		if (e.getSource()==reverse){
    			
    			int num=Integer.parseInt(enterno2.getText());
    		int r;
    		while (num!=0){
    		
    		 r=num%10;
    			num=num/10;
    			rev+=r;
    		}
    		}
    			result2.setText(rev);
    	
    		}
    	}

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Java Code:
    if(text.equals(new StringBuilder(text).reverse().toString())){
       System.out.println("A palindrome!");
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Store the original number to a string, store the reversed number to another string. Now compare these two strings, if equal makes a palindrome. Correct me if I am wrong.
    Swastik

  4. #4
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Java Code:
    if(text.equals(new StringBuilder(text).reverse().toString())){
       System.out.println("A palindrome!");
    }
    dude i don get this ! would ya put into my code so i can understand it better ?tnx

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    WTF?! Put it in there yourself. If you don't understand it you might want to look at the API docs.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    alright , at least tell me in which part of below code , im doin wrong :

    Java Code:
    /**
     * @(#)actionListener.java
     *
     * actionListener Applet application
     *
     * @author 
     * @version 1.00 2010/4/21
     */
     
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class reverse extends Applet implements ActionListener {
    
    	private Label enterno1,result1,palindrome;
    	private TextField enterno2,result2;
    	private Button reverse,clear;
    		
    	public void init() {
    		
    	enterno1=new Label(" Enter your number : ");
    	result1=new Label(" Reverse Form");
    	reverse=new Button("REVERSE ! ");
    	clear=new Button("CLEAR");
    	enterno2=new TextField(10);
    	result2=new TextField(10);
    	palindrome=new Label("Palindrome");
    	
    	add(enterno1);
    	add(enterno2);
    	
    	add(result1);
    	add(result2);
    	
    	add(reverse);
    	add(clear);
    	add(palindrome);
    	
    	
    	reverse.addActionListener(this);
    		clear.addActionListener(this);
    		
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		String rev="";
    		if (e.getSource()==reverse){
    			
    			int num=Integer.parseInt(enterno2.getText());
    		int r;
    		while (num!=0){
    		
    		 r=num%10;
    			num=num/10;
    			rev+=r;
    		}
    		}
    		
    		if(result2==(new StringBuilder(rev).reverse().toString())){
       		palindrome.setText("A palindrome!");
    }
    			result2.setText(rev);
    	
    		}
    	}

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    The problem is that you try to compare String with TextFields. That doesn't make sense.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    so what am i suppose to do then ? what to change in code?

  9. #9
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    if(result2.getText().equals(new StringBuilder(rev).reverse().toString())){
    Swastik

Similar Threads

  1. Test for Palindrome
    By arson09 in forum New To Java
    Replies: 11
    Last Post: 09-29-2010, 08:57 PM
  2. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 04:32 AM
  3. palindrome problem
    By Blaedel in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 01:52 AM
  4. Palindrome problem
    By Mika in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 03:46 PM
  5. Palindrome Test
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 02:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •