find a word in a string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xetulul
    New Member
    • Sep 2008
    • 1

    find a word in a string.

    my problem is that im unable to match a user input word to 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." .


    [CODE=java]import java.util.*;
    import java.io.*;
    import java.util.Strin gTokenizer;


    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.prin tl
    public void menu() throws Exception
    {
    System.out.prin tln("Enter option --> ");
    String cmd=myScanner.n ext();

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

    System.out.prin t("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.prin t(" "+i); }
    }
    m=m.next;
    i++;
    }
    }

    }[/CODE]
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your code doesn't even compile; you have to fix that first.

    kind regards,

    Jos

    Comment

    Working...