Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Compare Tokenized string element to a string array element

Posted on 2005-04-02
Medium Priority
Last Modified: 2013-12-29
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.io.*;
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
Question by:JayTreDoe357
  • 3
LVL 92

Expert Comment

ID: 13689671
use equals()
LVL 92

Accepted Solution

objects earned 500 total points
ID: 13689672
if (dictionary[0].equals(cToken)) System.out.println ("Finally");

Author Comment

ID: 13689682
Thanks, Man was that a n00b question!
LVL 92

Expert Comment

ID: 13689683
== does object equality
equals() tests for string equality

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question