Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 2nd, 2005, 3:15 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Searching a txt file

how can i search for a specif word in a txt file?
Dark Flare Knight is offline   Reply With Quote
Old Jul 2nd, 2005, 5:42 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Read it in line by line, getting one entie line as a String, and then use the contains() method from the String class on it to see if it's there or not. Or do you want something more specific?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jul 2nd, 2005, 6:35 PM   #3
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
String l = "Light";
f (filedata.contains (l))
            c.println ("Congratualations, your code is correct");
        else
            c.println ("Too bad, your code has bugs");
would this work? if yes, its not working.

Last edited by Dark Flare Knight; Jul 2nd, 2005 at 7:00 PM.
Dark Flare Knight is offline   Reply With Quote
Old Jul 2nd, 2005, 7:41 PM   #4
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
I get the following error:
Quote:
No method named "contains" was found in the type "java.lang.String".
Dark Flare Knight is offline   Reply With Quote
Old Jul 2nd, 2005, 9:08 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Post your whole code. I think that should work.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jul 2nd, 2005, 9:11 PM   #6
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
import java.awt.*;
import java.io.*;
import hsa.Console;
public class Duel_Prac
{
    static Console c = new Console ();
    static BufferedReader file;
    static String f_name;
    static String l_name;
    static String option = "";
    static String filedata;
    static public void main (String[] args) throws IOException
    {
        startup_page ();
        main_menu ();
    }


    static void startup_page ()
    {
        c.clear ();
        c.println ("Prac");
        c.println ();
        c.println ("Before we begin, please provide us with the following information about you: ");
        c.println ();
        c.print ("First Name: ");
        f_name = c.readLine ();
        c.print ("Last Name: ");
        l_name = c.readLine ();
    }


    static void main_menu () throws IOException
    {
        c.clear ();
        c.println ("Prac");
        c.println ();
        c.println ();
        c.println ();
        c.print ("1) shop");
        do
        {
            c.setCursor (3, 1);
            c.print ("What do you want do ? ");
            option = c.readLine ();
            if (option.equals ("1") == true || option.equalsIgnoreCase ("shop") == true)
                break;
        }
        while (!option.equals ("1") && !option.equalsIgnoreCase ("shop"));
        if (option.equals ("1") == true || option.equalsIgnoreCase ("shop") == true)
            shop ();
    }


    static void shop () throws IOException
    {
        try
        {
            file = new BufferedReader (new FileReader ("Card Data/EP1-EN.masterlist"));
            while ((filedata = file.readLine ()) == null)
                break;
            file.close ();
        }
        catch (IOException e)
        {
            c.println ("Data is corrupt");
        }
        String l = "Light";
        if (filedata.contains (l))
            c.println ("Congratualations, your code is correct");
        else
            c.println ("Too bad, your code has bugs");
    }
}
Dark Flare Knight is offline   Reply With Quote
Old Jul 2nd, 2005, 10:53 PM   #7
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
try
        {
            file = new BufferedReader (new FileReader ("Card Data/EP1-EN.masterlist"));
            while ((filedata = file.readLine ()) == null)
                break;
            file.close ();
        }
The way you'r reding the file, filedata will only contain the last line, actually i don't know if it reads anything because of : == null
I think it should be like this:
String currentLine;
 file = new BufferedReader (new FileReader ("Card Data/EP1-EN.masterlist"));
            while ((currentLine = file.readLine ()) != null)
              filedata += currentLine;
            file.close ();
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jul 3rd, 2005, 4:43 AM   #8
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
xP i sorta forgot at the time what the code was for it.

EDIT

still gives the same error

Quote:
No method named "contains" was found in the type "java.lang.String".
would it make a difference if i use equals () or equalsIngoreCase () instead of contains ().
Dark Flare Knight is offline   Reply With Quote
Old Jul 3rd, 2005, 1:07 PM   #9
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
I think that contains() is only for Java 1.5 so make sure that u have that.
The thing is i have never used contains() so i'm not sure how it works

If u're trying to find a specific word in a text, then you could do it like this :
Split the text in lines with BufferedReader, the use StringTokenizer to split the lines in words. Then you can use equalsIgnoreCase().

Google for a few examples of StringTokenizer ... and you should be done in no time.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jul 3rd, 2005, 4:06 PM   #10
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
I downloaded 1.5 but how do i use it?
Dark Flare Knight 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 5:17 AM.

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