473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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;
  4.  
  5.  
  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;
  17.  
  18.     Scanner myScanner=new Scanner(System.in);
  19.  
  20.    System.out.printl
  21.    public void menu() throws Exception
  22.    {
  23.       System.out.println("Enter option --> ");
  24.       String cmd=myScanner.next();
  25.  
  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;
  48.  
  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;
  56.  
  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.    }
  68.  
  69. }
Sep 25 '08 #1
1 3942
JosAH
11,448 Expert 8TB
Your code doesn't even compile; you have to fix that first.

kind regards,

Jos
Sep 26 '08 #2

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

Similar topics

2
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...
5
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...
2
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...
19
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...
2
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...
1
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:...
10
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...
5
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...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.