472,976 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

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." .

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import java.util.StringTokenizer;
  6. class edit 
  7. {
  8.    class Node 
  9.    {
  10.       Node next;
  11.       String text;
  12.    }
  13.     private Node head;
  14.     boolean openB=false;
  15.     Node m=new Node();
  16.     int i;
  18.     Scanner myScanner=new Scanner(System.in);
  20.    System.out.printl
  21.    public void menu() throws Exception
  22.    {
  23.       System.out.println("Enter option --> ");
  24.       String cmd=myScanner.next();
  26.       if(cmd.equals("open"))
  27.       {
  28.          linecount=0;
  29.          System.out.print("Enter file name : ");
  30.          fname=myScanner.next();
  31.          openB=open(fname);
  32.       }
  33.       else if(cmd.equals("find"))
  34.       {
  35.          System.out.print("Text to find? ");
  36.          String textFind=myScanner.next();
  37.          System.out.println("ontop of method call: "+textFind);
  38.          find(textFind);
  39.       } 
  40.       else
  41.          break;
  42.    }
  43.    public void find(String s) 
  44.    {
  45.       m=head;
  46.       i=1;
  47.       String temp, line;
  49.       System.out.print("Found on line(s): ");
  50.       while(m!=null) 
  51.       {
  52.           if(i==1)
  53.              line=head.text;
  54.           else
  55.              line=m.text;
  57.           StringTokenizer tokenizer=new StringTokenizer(line);
  58.           while(tokenizer.hasMoreTokens())
  59.           { 
  60.                temp=tokenizer.nextToken();
  61.                if( temp.equals(s))
  62.                { System.out.print(" "+i); }
  63.           }
  64.           m=m.next;
  65.           i++;
  66.       } 
  67.    }
  69. }
Sep 25 '08 #1
1 3920
11,448 Expert 8TB
Your code doesn't even compile; you have to fix that first.

kind regards,

Sep 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
by: Paula | last post by:
Hi !! I have to find some words in a string. I can use string.IndexOf, LastIndexOf, etc, but they are case sensitive. And there is another problem : If I found the word, I have to get three...
by: Al_P via DotNetMonster.com | last post by:
I have: Win2K Office2000 Working in VB.Net (2003) My .Net project has a reference to Word 9.0 Instantiating things in a simple and straightforward manner: Dim word as Word.Application Dim...
by: ashmangat | last post by:
Hi! now on the chapter "string-class" My assignment is below Word Counter: Write a function that accepts a pointer to a C-String as an argument and returns the number of words contained in the...
by: Gaijinco | last post by:
I was trying a function that read an string (which previously had attached two special characters & for the beginning of the word and # for the end of the word) and if it was "<word>ize" it changed...
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
by: Johny | last post by:
I need to find all the same words in a text . What would be the best idea to do that? I used string.find but it does not work properly for the words. Let suppose I want to find a number 324 in...
by: Chris Mahoney | last post by:
Hi I have a string containing a phrase, and I want to search for a particular word and get its index. I've tried a couple of things already: InStr(phrase, word) - this doesn't work because it...
by: zaza24 | last post by:
Hello there I am loosing my time since a few days trying to install a CRM (ACT! 2008 version 10) software on a brand new HP notebook with Vista prof OS. Ths installation do not show any...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.