java - Find a string and display the line number and the line itself? -



java - Find a string and display the line number and the line itself? -

i'm designing programme reads size of txt file, number of words, numbers, , characters contains, , ability search file given string.

for example.

a txt file has

there 1 time land

that land no longer exists.

that land was...

if wanted search "land" list 3 lines.

the methods created search file , find string are:

public string searchword(string key) throws exception { linenumberreader lnr = new linenumberreader(new filereader(f)); string line = null; line = recursivesearch(lnr.readline(), key, lnr); homecoming line; } public string recursivesearch(string currentlinetext, string key, linenumberreader lnr) throws exception { if (currentlinetext != null) { string lcase = currentlinetext.tolowercase(); if (lcase.contains(key.tolowercase())) { homecoming ("line " + lnr.getlinenumber() + ": " + currentlinetext + "\n"); } } homecoming recursivesearch(lnr.readline(), key, lnr); }

the programme indeed search through files properly, have assumed searchword working properly. however, if type "land" homecoming "there 1 time land" rather 3 lines.

if type "longer" homecoming sec line properly. directions can give me show required lines?

edit

i've went madprogrammer's advice, , believe right. list needed. however, after re-did code

public list<string> searchword(string key) throws exception { linenumberreader lnr = new linenumberreader(new filereader(f)); homecoming recursivesearch(lnr.readline(), key, lnr); } public list<string> recursivesearch(string currentlinetext, string key, linenumberreader lnr) throws exception { list<string> resultlist = new arraylist<string>(); if (currentlinetext != null) { string lcase = currentlinetext.tolowercase(); if (lcase.contains(key.tolowercase())) { string result = ("line " + lnr.getlinenumber() + ": " + currentlinetext + "\n"); resultlist.add(result); } } string nextline = lnr.readline(); if (nextline != null) { resultlist.addall(recursivesearch(nextline, key, lnr)); } homecoming resultlist; }

the test class came error. programme did not find "searchmethod"

f.selectfile(); if (f.exists()) { seek { string input = joptionpane.showinputdialog("enter word "); (string line : searchword(input)) { display(line); }

i need rewrite portion of test class, or more illustration of lists improve understanding.

edit 2

so sent 2 methods in test class , renamed them "static" working. prefer not that, gotta have filereader take file user inputs, should easy me. guys.

the problem search routines guard find single word...

return ("line " + lnr.getlinenumber() + ": " + currentlinetext + "\n");

will stop farther execution , homecoming caller, never looking new results. fact homecoming string problem, can never find more single string, unless intend concatenate results

a improve solution allow method homecoming array or list of results, adding each result list it's found , adding results found subsequent calss recursivesearch, example...

import java.io.file; import java.io.filereader; import java.io.linenumberreader; import java.util.arraylist; import java.util.list; public class test1 { public static void main(string[] args) { seek { (string line : searchword("land")) { system.out.println(line); } } grab (exception exp) { exp.printstacktrace(); } } public static list<string> searchword(string key) throws exception { linenumberreader lnr = new linenumberreader(new filereader(new file("stufftoread.txt"))); homecoming recursivesearch(lnr.readline(), key, lnr); } public static list<string> recursivesearch(string currentlinetext, string key, linenumberreader lnr) throws exception { list<string> results = new arraylist<string>(25); if (currentlinetext != null) { string lcase = currentlinetext.tolowercase(); if (lcase.contains(key.tolowercase())) { results.add("line " + lnr.getlinenumber() + ": " + currentlinetext); } results.addall(recursivesearch(lnr.readline(), key, lnr)); } homecoming results; } }

java file recursion

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -