Results 1 to 5 of 5
  1. #1
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default I need help on my code

    im trying to make a program that takes a string. Then, the user enters a word, and the program will search the previous string for the word. If the word is in the string, it will say Word found. If the word isn't in the string, it will say word not found.
    Right now though, the program will only read the first word of the sentence. If i entered "jason was here", it will only read the jason. how do i make the program read the whole string?


    CODE:

    import java.util.*;
    import javax.swing.*;
    import java.io.*;

    public class Strtok{

    public static void main(String[] args){
    String demo = JOptionPane.showInputDialog (null, "Please enter a String", "Enter String", JOptionPane.QUESTION_MESSAGE);
    String word = JOptionPane.showInputDialog (null, "Please enter the word you are looking for", "Find", JOptionPane.QUESTION_MESSAGE);

    StringTokenizer st = new StringTokenizer(demo);




    if((st.nextToken()).equals (word)){
    JOptionPane.showMessageDialog (null, "Yes, the word is in the String", "Word is found!", JOptionPane.INFORMATION_MESSAGE);
    }

    else{
    JOptionPane.showMessageDialog (null, "No, the word isn't in the String", "Word is not found!", JOptionPane.INFORMATION_MESSAGE);
    }


    }
    }

    some help would be really nice.

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Have u tried using another String reader class?
    Buffered reader maybe?
    Seems like it stop readeing when it finds the space

  3. #3
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    im not advanced yet. how do i run two classes at the same time if i was to make a string reader class with this class. Or do you mean turn this class into a bufferReader class.

  4. #4
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    You need to do something like this:
    Java Code:
    boolean found = false;
    while (st.hasMoreElements())
    {
    if (st.nextToken().equals(word))
      found = true;
    }
    
    if (found)
      //say it was found
    else
      //say it was not found

  5. #5
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Need help with my code.
    By stormviper in forum New To Java
    Replies: 8
    Last Post: 11-18-2013, 04:58 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  3. Pls some one to help mi wit this code
    By _nik_ in forum New To Java
    Replies: 3
    Last Post: 02-10-2008, 03:02 AM
  4. tic tac toe code
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 05:36 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •