java - How would I take a TextField input, take each letter, and put it into chars? -


i trying make code gui allows user enter word in jtextfield , have check if it's palindrome (i.e ono palindrome, same , forth). how take jtextfield , convert them chars check if sentence palindrome? (and also, how remove spaces, punctuation, etc?) current code set up

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.stack;  /**  * write description of class palidromania_kevin here.  *   * @author kevin hy  * @version 09/23/13  */ public class palindromania extends jframe {     public jpanel panel1,panel2;     public jlabel main1;     public jbutton check;     public jtextfield enterpal;     public string inputword,letter;     public int num;     public char checker;     public stack<string> stack = new stack<string>();     public palindromania ()     {         super ("palindromania checker");         setsize (1024,768);         container container = getcontentpane();          panel1 = new jpanel();              panel1.setbackground (new color(10,50,50));         panel1.setlayout (new flowlayout());         panel2 = new jpanel();              panel2.setbackground (new color(255,255,255));         panel2.setlayout (new flowlayout());          main1 = new jlabel ("palindromania checker");          check = new jbutton("verify palindrome");         buttonhandler handler = new buttonhandler();         check.addactionlistener(handler);         enterpal = new jtextfield(8);         container.add(panel1,borderlayout.north);         container.add(panel2,borderlayout.center);         panel2.add(enterpal);         panel2.add(check);         setvisible(true);     }      public static void main (string [] args)     {         palindromania application = new palindromania ();        }     public class buttonhandler implements actionlistener     {         public void actionperformed (actionevent event)     {         if (event.getsource () == check)//check if palindrome button         {             inputword="";letter="";             inputword=enterpal.gettext();             inputword=inputword.tolowercase();             inputword=inputword.replace("[ !'-,]","");             (int x=0; x<=inputword.length()-1;++x)//inputs lettes stack             {                 checker=inputword.charat(x);                 letter+=checker;                 stack.add(letter);                 letter="";                 joptionpane.showmessagedialog (null, stack.peek());             }              (int x=0; x<=inputword.length()-1;++x)//inputs lettes stack             {                 checker=inputword.charat(x);                 letter+=checker;                 stack.add(letter);                 if (letter.equals(stack.pop()))                 num+=1;                 letter="";             }             if (num==inputword.length())             joptionpane.showmessagedialog (null, "you have palindrome!");             else             joptionpane.showmessagedialog (null, "this not palindrome, sorry!");         }     } } 

}edit: modified code, made mistake parse strings, , can char letters go in test, , can words appear go char , outputted

edit2: main issue now, though can add stuff stack, how come .replace/.tolowercase on string don't anything? strings enter in textfield not replaced (i.e hello world stays hello world, not helloworld), , there null in stack, though taking letters inputword? (i.e hello nullolleh) tried put string

compare+=stack.pop(); 

but adds letters null each? (nullnullo, nullol nulloll) why have null?

edit3: works! bit tedious way had because have use stack compare (for learning purposes)

there few mistakes in code :

  1. first touppercase() doesn't work because need assign result of call variable. replace : inputword.tolowercase(); : inputword = inputword.tolowercase();. comes string being immutable in java.

  2. you can simplify code using simple regex. use inputword.replace("[ !',-]",""); instead of calls replace , again assign result variable. : inputword = inputword.replace("[ !',-]","");

  3. now have null because never initialize letter string. add little letter="" before loop , null should gone.

  4. and @ end use pop() on stack reversed string after loop , compare original one. :

    if(inputword.equalsignorecase(stack.pop())){        joptionpane.showconfirmdialog(null, "this palindrome", "is palindrome ?", joptionpane.yes_option, joptionpane.information_message); } else {        joptionpane.showconfirmdialog(null, "this not palindrome", "is palindrome ?", joptionpane.yes_option,joptionpane.error_message); } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -