Compare Tokenized string element to a string array element

First of all YES this is homework, but I just need some help with comparing a string to an element in a string array. The string array is dictonary[] and the string is cToken (which was pulled from a string buffer). I am tring to compare to see if they are the same. I tried testing against dictonary[0] which is "I" with 1 token typed in as "I" but they are not equal? any idea why? I just need help comparing the two.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Project2 extends JFrame
   public JLabel Label1;
   public JTextField Sentence;
   public JTextArea txtArea;
   public JButton Check;
   //The dictionary
   public static String dictionary[] = {"I", "you", "he", "she", "it", "we", "they","me"};
   //The alphabet
   public static String letters[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
   public StringBuffer fSentence = new StringBuffer();
   // set up GUI
   public Project2()
      super( "Project2 - a simple spell checker utility" );

      // get content pane and set its layout
      Container c = getContentPane();
      c.setLayout( new FlowLayout() );

      // create Lable1
      Label1 = new JLabel( "Please enter a sentence:") ;
      c.add ( Label1 );
      // create Text Field
      Sentence = new JTextField(45);
      c.add ( Sentence );
      //create button to check spelling
      Check = new JButton("Check Spelling");
      c.add (Check);
      // create Text Area
      txtArea = new JTextArea(10,20);
      c.add ( txtArea);
      // create an instance of inner class ButtonHandler
      // to use for button event handling
      ButtonHandler handler = new ButtonHandler();
      Sentence.addActionListener ( handler);
      Check.addActionListener ( handler );

      //set size of window and if it is visible
      setSize( 640, 480 );
      setVisible( true );

   } // end ButtonTest constructor
   public void paint( Graphics g )
               super.paint(g); //call superclass's paint method
   } //end method paint
   public static void main( String args[] )
      Project2 application = new Project2();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   // inner class for button event handling
   private class ButtonHandler implements ActionListener

      // handle button pressed or enter key pressed event
      public void actionPerformed( ActionEvent event )
         String fullSentence = new String(Sentence.getText());
         StringTokenizer tokens = new StringTokenizer (fullSentence);
          txtArea.setText ("Now running simple spell checker...\n");
          txtArea.append ( "Number of words to check :" + tokens.countTokens());
          while ( tokens.hasMoreTokens())
                     String cToken = new String(tokens.nextToken());
                     System.out.println (dictionary[0]);
                     System.out.println (cToken);
                     txtArea.append ( "\nCurrently searching for: " + cToken);
                     if (dictionary[0] == cToken) System.out.println ("Finally");
         repaint(); // Repaints the applet when the draw button is pressed

   } // end private inner class ButtonHandler

} // end class Project2
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

use equals()
if (dictionary[0].equals(cToken)) System.out.println ("Finally");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JayTreDoe357Author Commented:
Thanks, Man was that a n00b question!
== does object equality
equals() tests for string equality
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.