Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a large file of about 10 MB, I want to search a specific string, and this specific string may be used a lot of times in 10 Mb text file. I need results where this specific string is used. I want to do search like Google. For example when i write a string then google comes with matching Patterns . Your suggestions will be appreciated.
file formate
he is going to school.
we should do best deeds.
we should work hard.
.
.
.
.
Always speak truth.
i have search edit field in my application.
user write "should" in search edit field.and press search button.
a list should be opened in which searched words come with it's complete line.
for example result should be
we should do best deeds.
we should work hard.
A simple way to search a file and get a match "with context" is to use grep. For example, to match every line with "hello", and print one line before and three lines after, you would do
grep -b1 -a3 'hello' myBigFile.txt
You can use grep -E to allow for a wide range of PCRE regex syntax.
Without more detail it would be hard to give you a better answer.
EDIT 2
Now that you have explained your problem more clearly, here is a possible approach:
InputStream fileIn;
BufferedReader bufRd;
String line, pattern;
pattern = "should"; // get the pattern from the user, do not hard code. Example only
fileIn = new FileInputStream("myBigTextfile.txt");
bufRd = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
while ((line = bufRd.readLine()) != null) {
if(line.contains(pattern)) {
System.out.println(line); // echo matching line to output
}
}
// Done with the file
br.close();
If you need to match with wildcards, then you might replace the line.contains with something that is a little more "hard core regex" - for example
matchPattern = Pattern.compile("/should.+not/");
(only need to do that once - after getting input, and before opening file) and change the condition to
if (matchPattern.matcher(line).find())
Note - code adapted from / inspired by https://stackoverflow.com/a/7413900/1967396 but not tested.
Note there are no for loops... maybe the boss will be happy now.
By the way - if you edit your original question with all the information you provided in the comments (both to this answer and to the original question) I think the question can be re-opened.
If you expect the user to do many searches it may be faster to read the entire file into memory once. But that's outside of the scope of your question, I think.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a list of courses and I need to filter them while searching
I need to be able to search for every word the user enters
For example: if I have course called nutrition basics, If I enter in search bar basics nothing show up but if i enters nut or even n it works.
Here is my code:
itemFilter: (suggestion, input) => suggestion.name .toLowerCase() .startsWith(input.toLowerCase()),
How can I do the filter as I want? I need to search even if for letter 'b' and still get the result nutrition basics.
I'm using flutter and firebase.
Note: I tried contains insteadof startWith, but if i have two courses one nutrition basics and other math 500 basics and i searched for word basics the result shows after enter is only nutrition, also i need to be able to search for letters too, as b in basics or c..
Thanks.
You have used method startsWith
itemFilter: (suggestion, input) => suggestion.name .toLowerCase() .startsWith(input.toLowerCase()),
use contains instead of startsWith
itemFilter: (suggestion, input) => suggestion.name .toLowerCase() .contains(input.toLowerCase()),
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Both of the codes below give me the same exact answers. I was just wondering which would be better programming practice for readability and maintainability. Would less lines of code be best? Would one affect the functionality of the program more than the other? any suggestions would be very much appreciated as I just want to learn the best practices for programming.
Here are the codes:
for (int i = 0; i < db.getAllDecks().size(); i++)
{
String CardCount = String.format("(%s)",db.getCardsForDeck(i+1).size());
adapter2.add(db.getAllDecks().get(i).getDeck_name());
adapter3.add(CardCount);
}
or
for (Deck deck: deckList) {
String deckName = deck.getDeck_name();
adapter2.add(deckName);
int count = db.getCardIds(deck).length;
String strCount = Integer.toString(count);
adapter3.add(strCount);
}
Overall, I think the second code is clearer, and more readable.
It contains moe variable names that is able to tell what exactly it is used for, such as deckName, count and strCount. I can clearly see that you are getting every deck's name and card count and put them in different (list?) adapters.
For the first one, I apparently needed more time to comprehend what it is doing. So IMO, the second one!
Also if you could just rename getDeck_name to getDeckName that would be better for people to read. getDeckName follows the naming convention for naming Java methods i.e. camelCase.
if you want to get data from simple list thnn foreach loop is good to use but,,, if you want to data from exact position or to store from id than for-loop is better ..
and there is NO difference by performance wise both are same as well, as i know.
as my suggestion use for loop :)
As per this book Code Complete - Steve McConnell's
for loop is good choice when you need a loop that executes a specified number of times.
foreach loop is useful for performing an operation on each member of an array or the container.
for more visit : Google books - Code Complete
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I am kinda new to android with json, and the search.
I want to apply advanced search filter in my autocompletetextview / edittext, in android from json file stored in assets folder.
The json file is consists of FAQs for my app.
And now I want to create advanced search filter like in Google chrome, that if I am typing something like
STACK
and the predictions I get in URL bar is
STACK OVERFLOW
next row
STACK EXCHANGE
etc.
Where
stack
word is highlighted, with appropriate suggestions.
so, same that way I want to make my search filter that way, because there are some amount of FAQs in my json file, and when user types something like,
HOW PAPER IS MADE UP OF or METHOD OF PAPER MAKING or WANT TO MAKE
PAPER OUT OF WASTE or ETC.
I hope that you got my point, and now here from given hints,
the words made up, making, make, all should be meant for MAKE, a root word for all of them.
Because my FAQ has questions and related answers (obviously), so questions might be in long format, so to retrieve best similar response, I want to create the advanced search filter.
So, my question is,
IS THERE ANY PARTICULAR API OR ALGORITHM THAT I CAN APPLY TO ACHIEVE
THIS TASK?
I have researched for this for last almost 4-5 days..
What I have applied till now for reading json data is as below..
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("faq.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
and then,
try {
JSONObject jO = new JSONObject(loadJSONFromAsset());
JSONArray json = jO.getJSONArray("faq");
Log.v("faq", "faq");
for (int i = 0; i < json.length(); i++) {
final JSONObject e = json.getJSONObject(i);
q = e.getString("q");
responseList.add(q);
a = e.getString("a");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And then I set the arrayadapter.
Even I have worked with custom adapter, but that is not the issue now.
So, now in my search engine, when someone write set of keywords, the code should ignore the articles, adverbs etc.
The questions which are predicted must me sorted in relevance index.
The synonym capturing and sorting capabilities should be there.
Give the importance to recognized word to refine the relevant set of words.
May be able to add taxonomy dictionary.
So, I want this type of code / engine that can achieve the task required.
Even when user click spacebar, the sorting must be refreshed.
Is there any such API / library, that can fulfill the needs?
And frankly speaking, I have tried many of the custom filter
solutions, and ALGOLIA API (not open-source, had trial for 14 days
only..) and others too, but these are not capable enough to solve my
need.
Therefore, I thank you in advance, if you can help me to achieve my task with suitable solution of api or algorithm or code or idea..
Regards.
The LUCENE Apache API, for mobile version, 5.3.0 is the solution for my query, which I got finally..
The lucene search and lucene study are the different projects to deal with..
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an HTML file being utilized by webview in an activity. The HTML file makes up a large table that is scrollable in the webview. I want to add some javascript to the HTML file to float an image over the table. I also want this image to change positions on a time interval (every five seconds). I was hoping to accomplish such by using a random number generator to change the coordinates of the floated image every five seconds.
Firstly, is this implementation possible? I am new to javascript, but this doesn't seem like too far-fetched of an idea.
Secondly, I want the floated image to be clickable. And, upon clicking I want information to be placed in a string. My other question is, is there a way to call the string built by the HTML file into another activity in android?
I know my intentions may be unclear to some. If clarification is necessary, please ask. Thanks for any and all help :D
prob 1 : need an image to float
prob 2 : need the image to change direction at fixed time interval
first of all I would suggest you create a gif image which changes direction at every 5 seconds
thus your prob 2 is solved
now coming to first prob ,
use this
String HtmlFileString = "<html style=\"height:90%;width:95%\"><body>" +
"<img src=\""+**gif_char**+".gif"+"\" " +
"style=\"height:100%;width:100%\"></body></html>";
webview.loadDataWithBaseURL("file:///android_asset/",HtmlFileString,"text/html", "UTF-8","");
don't forget to keep the gif in asset folder
and also this gif variale you pass by yourself ,
it simply changes the name of your gif so you can show any image anytime you want
just call this line again with diff param for variable gif_char
if you dont want to use the GIF image , you can use a simple image and change the image by loading webview again and again with diff image passed every time
let me try to explain further
step 1 : read the html file using buffered reader ,
step 2 : store the file content in a string
step 3 : hange the image resource in html file string
step 4 : pass this html file string to method webview.loadDataWithBaseURL()
And also , Webview doesn't support onclick event but support ontouch event
To do this you will need a combination of Java and Javascript.
Inserting the image and moving it around will easiest in javascript similar to what is shown above.
To get the return string back out the android application you should look at using
addJavascriptInterface (Object object, String name)
This will allow the javascript to call public functions in the java object that you passed to this function.
This object could then transfer the string to a new activity using the extras bundle in the intent that is used to start the activity
This question already has an answer here:
How to extract this string variable in android?
(1 answer)
Closed 9 years ago.
String test=["1","Low-level programming language",true]
Here i want to extract this string value and as i need to get only second value like "Low-level programming language".How to get this value using string functions in android?
Per your comment, I'm assuming that you have a single string that contains the entire text (including the brackets). In general, splitting comma-separated values is a fairly tricky process. For your specific string, though, it's kind of easy:
String test = "[\"1\",\"Low-level programming language\",true]";
String[] pieces = test.split(",");
String middle = pieces[1];
// now strip out the quotes:
middle = middle.substring(1, middle.length() - 1);
In general, you might want to look at using a general CSV parser like Apache Commons CSV or openCSV.
Alternatively, if this is JSON data (which looks more likely than CSV), take a look at using one of the Java JSON libraries listed here (scroll down the page to see the list).