Welcome to Dream.In.Code
Become a Java Expert!

Join 150,198 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,995 people online right now. Registration is fast and FREE... Join Now!




find a word in a string

 
Reply to this topicStart new topic

find a word in a string

xetulul
25 Sep, 2008 - 12:44 PM
Post #1

New D.I.C Head
*

Joined: 7 Sep, 2008
Posts: 9


my problem is that im unable to find a word in a string. the strings are in a file and then placed in nodes. i have to go through each token in each node to find the 'word'.
but my code doesn't work. how can i make my code to understand that word: "night" == token in node: "night." .

help please

CODE

import java.util.*;
import java.io.*;
import java.util.StringTokenizer;


class edit
{
   class Node
   {
      Node next;
      String text;
   }
    private Node head;
    boolean openB=false;
    Node m=new Node();
    int i;

    Scanner myScanner=new Scanner(System.in);

   System.out.printl
   public void menu() throws Exception
   {
      System.out.println("Enter option --> ");
      String cmd=myScanner.next();

      if(cmd.equals("open"))
      {
         linecount=0;
         System.out.print("Enter file name : ");
         fname=myScanner.next();
         openB=open(fname);
      }
      else if(cmd.equals("find"))
      {
         System.out.print("Text to find? ");
         String textFind=myScanner.next();
         System.out.println("ontop of method call: "+textFind);
         find(textFind);
      }
      else
         break;
   }
   public void find(String s)
   {
      m=head;
      i=1;
      String temp, line;

      System.out.print("Found on line(s): ");
      while(m!=null)
      {
          if(i==1)
             line=head.text;
          else
             line=m.text;

          StringTokenizer tokenizer=new StringTokenizer(line);
          while(tokenizer.hasMoreTokens())
          {
               temp=tokenizer.nextToken();
               if( temp.equals(s))
               { System.out.print(" "+i); }
          }
          m=m.next;
          i++;
      }
   }

}


User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Find A Word In A String
25 Sep, 2008 - 01:04 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
There are some syntax issues with your code, there is an extra } near the bottom, the variables fname and linecount aren't defined anywhere and where is your main method?

I assume the code isn't compiling? or are you having errors that you forgot to share with us?
User is offlineProfile CardPM
+Quote Post

xetulul
RE: Find A Word In A String
25 Sep, 2008 - 01:46 PM
Post #3

New D.I.C Head
*

Joined: 7 Sep, 2008
Posts: 9

my program is much longer than this, so i thought, just place the part of program im having trouble with.
my program compiles and works, except for this find() method. sorry if i left out any declaration of variables for you to see.


CODE



class editDriver
{
    public static void main(String[] args)throws Exception
    {
        edit e = new edit();
        e.menu();
    }
}







User is offlineProfile CardPM
+Quote Post

puttyman
RE: Find A Word In A String
26 Sep, 2008 - 07:20 PM
Post #4

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 4

Familiar with regular expressions ? Have a look at the java class Pattern
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:49AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month