473,804 Members | 4,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find a word in a string.

1 New Member
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 3966
JosAH
11,448 Recognized Expert MVP
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
14941
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 in the format that I need it in order to create the reports that we use. So far this has proven to be successful for the reports that I am doing and the data that I am pulling into it. I just have one challenge that may require a lot of work and I...
5
7842
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 words before and after the found word . Example:
2
2182
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 doc As Word.Document Dim range As Word.Range
19
9592
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 string. For instance, if the string argument is "Four score and seven years ago" the function should return the number 6. Demonstrate the function in a program that asks the user to input a string and then passes it to the function. The number of...
2
1762
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 into "change into <word>". I did this function: bool Ize(string& s) { bool is=false; string eval="ize"; int end=s.find('#');
1
4219
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: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
10
3924
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 the text '45 324 45324' there is only one occurrence of 324 word but string.find() finds 2 occurrences ( in 45324 too)
5
11710
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 finds "subwords", eg. If I search for "colour" in a phrase containing "colourful", it'll still detect it.
0
2771
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 troubles and works fine; the software installs also Microsoft SQL Server Express 2005 (the different services installed are SQL Server for ACT7, SQL server Active Directory Helper, SQL Server Browser and SQL Server VSS Writer). SQL Service for ACT7 is in...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.