![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Searching a txt file
how can i search for a specif word in a txt file?
|
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
String l = "Light";
f (filedata.contains (l))
c.println ("Congratualations, your code is correct");
else
c.println ("Too bad, your code has bugs");Last edited by Dark Flare Knight; Jul 2nd, 2005 at 8:00 PM. |
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
I get the following error:
Quote:
|
|
|
|
|
|
|
#5 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
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");
}
} |
|
|
|
|
|
#7 |
|
Professional Programmer
|
try
{
file = new BufferedReader (new FileReader ("Card Data/EP1-EN.masterlist"));
while ((filedata = file.readLine ()) == null)
break;
file.close ();
}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 ! |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
xP i sorta forgot at the time what the code was for it.
EDIT still gives the same error Quote:
|
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
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 ! |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
I downloaded 1.5 but how do i use it?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|