Results 1 to 9 of 9
- 12-04-2008, 12:14 PM #1
searchin for more Strings in a Text
Please, I need your HELP!! In my Program, I open a file, read it line by line and find the word „Page“in it.
Now I will that my Program find more than 1 word. :confused:
Thanks for your Help!Java Code:. . String line = dis.readLine(); int linem =1; while (line != null) { String searchText = "Page"; if(line.indexOf(searchText) !=-1) // or if(line.contains(searchText)) { underline(line,searchText); } linem++; line = dis.readLine(); } . .
- 12-04-2008, 04:00 PM #2
You mean you want to find multiple occurances of Page; or you want to find different words(Page, Chapter) at the same time?
- 12-05-2008, 04:29 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You want to find only one? Can you explain it more clearly. If a line of the file as follows what's your expectation as the result.
This page has more page words.
- 12-05-2008, 09:22 AM #4
yes I need to find more words. That means in your (Eranga) sentence :
I want to find : page and word (for example)This page has more page words.
Note !!! ::eek:
1. my program underlines the words.
2. I dont want to give out how many times the file contains the word.
thank you for your Help!
- 12-05-2008, 01:38 PM #5
loop and array
- Populate an array with the words you want to look for
- Create a "for" loop that takes each word from the array and compares against the sentence (like you do currently)
- Maybe something like:
Luck,Java Code:For (i=0; i < myArray.length; i++) { Underline stuff; }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-05-2008, 02:07 PM #6
Thanks, it remind me of a good solution :
String searchText = "Page";
String searchText1 = "State";
if((line.indexOf(searchText) !=-1) || (line.indexOf(searchText1) !=-1))
{
underline(line,searchText);
underline(line,searchText1);
}
its not a nice way but it does the work. I prefere to use Regular Expression but still don't know hoe the method works !!:cool:
- 12-05-2008, 02:45 PM #7
That's not a good solution
That is not a good solution. It's functional, but not good. The way it is coded, only one of the conditions of the "if" statement has to be true for it to enter the code in the "if" and I don't know if the underline method can handle a condition where the word is NOT in the sentence.
Again, a more robust solution would be to use an array and a loop (ex: for loop). These are not complex commands and very useful.
The "for" loop would look something like:
Some links:Java Code:For (i=0; i < myArray.length; i++) { if (line.indexOf(myArray[i] != -1) { underline(line,myArray[i]); } }
Arrays:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
for loops:
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
Java SE API 6:
Java Platform SE 6
Sun Java tutorial:
The Java™ Tutorials
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-08-2008, 10:11 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-09-2008, 08:51 AM #9
to CJSLMAN :
thanks for the links but I think instead of Arrays I could use List<String>! do you know a link about it too? or could you give me an example?
how can I use it in my Prog.?Java Code:List<String> woerter = new ArrayList<String>();
to Eranga:Java Code:String line = dis.readLine(); int linem =1; while (line != null) { System.out.println(line); String searchText ="Page" ; String searchText1 ="State" ; if((line.contains(searchText))||(line.contains(searchText1))){ //if(line.indexOf(searchText) !=-1){ underline(line,searchText); underline(line,searchText1); } linem++; line = dis.readLine();
I kann find the words now and underline them too. But what I want is to find more Words !!
Similar Threads
-
printing simple text as text on printer
By Nicholas Jordan in forum Advanced JavaReplies: 0Last Post: 12-01-2008, 01:42 AM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
text box listeners and returning multiple strings from methods
By int80 in forum New To JavaReplies: 5Last Post: 07-18-2008, 04:30 PM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM -
text Strings to produce letters
By dc2acgsr99 in forum New To JavaReplies: 7Last Post: 01-29-2008, 08:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks