?
Solved

Compare Tokenized string element to a string array element

Posted on 2005-04-02
4
Medium Priority
?
250 Views
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.

Thanks,
Jason



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);
      txtArea.setEditable(false);
      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
0
Comment
Question by:JayTreDoe357
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13689671
use equals()
0
 
LVL 92

Accepted Solution

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

Author Comment

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

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 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