Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 14th, 2005, 5:51 PM   #11
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
so would this be right?

            BufferedReader buf = new BufferedReader (new FileReader ("User Data/Master List.txt"));
            while ((currentLine = buf.readLine ()) != null)
            {
                filedata += currentLine;
                StringTokenizer st = new StringTokenizer (filedata);
                while (st.hasMoreTokens ())
                {
                    str = st.nextToken ();
                    asd = str;
                }
            }
            buf.close ();
Dark Flare Knight is offline   Reply With Quote
Old Jul 15th, 2005, 12:57 AM   #12
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 406
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
No, it wouldn't be right
Quote:
    filedata += currentLine;
                StringTokenizer st = new StringTokenizer (filedata);
What hapens here is : The string filedata is added a line then it's tokenized, then it's added another line and the entire string is tokenized all over again.
Quote:
      str = st.nextToken ();
                    asd = str; //what is this sopose to do ? 
Here's how I would do it:
import java.io.*;
import java.util.*;

class SearchFile{
	
	public SearchFile(String txt, String file){
		
		int count = 0; // count how many times the word apears
			
		String currentLine;
		try{
			BufferedReader bfr = new BufferedReader(new FileReader(file));
			while ((currentLine = bfr.readLine ()) != null){
				//take one line at a time and split it in words
        	StringTokenizer str = new StringTokenizer(currentLine);
        		while (str.hasMoreElements()){
        			String element = str.nextToken();
        			//see for every element if it's the one you're searching for.
        			if(txt.equalsIgnoreCase(element)){
        				count++;
        			}
				}
			}
		System.out.println("Word found "+count+" times");

		}catch(IOException e){
			e.printStackTrace();
		}		
	}	
}
class MainSearch{
	public static void main(String[] args){
		SearchFile srch= new SearchFile("Romy","file.txt");
	}
}
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jul 15th, 2005, 3:27 AM   #13
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
	public String stripHTMLTags( String message ) {
    	StringBuffer returnMessage = new StringBuffer(message);
	    int startPosition = message.indexOf("<"); // encountered the first opening brace
    	int endPosition = message.indexOf(">"); // encountered the first closing braces
	    while( startPosition != -1 ) {
    	  	returnMessage.delete( startPosition, endPosition+1 ); // remove the tag
      		startPosition = (returnMessage.toString()).indexOf("<"); // look for the next opening brace
      		endPosition = (returnMessage.toString()).indexOf(">"); // look for the next closing brace
    	}
    	return returnMessage.toString();
  	}

a bit of code i ripped from what i am doing now, this code works btw

if you use the indexOf() method it finds the position in the string where the string contains the string you are looking for, or returns -1 if it is invalid.


edit: anopying the code tags messed up my tabbing
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jul 15th, 2005, 11:54 AM   #14
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
ok and how would i list the files in a directory?
Dark Flare Knight is offline   Reply With Quote
Old Jul 15th, 2005, 1:18 PM   #15
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 406
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
google
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC