Java code to find a string in a text file


















Note: it is expected that key and value pairs are limited to 59 characters and separated by " ". If a value is part of the previous key, the line should also start with " " — Ian2thedv. Paul Richter Paul Richter 5, 2 2 gold badges 17 17 silver badges 22 22 bronze badges. Because in text file can be several empty spaces in substring 0, 9. Sharad Gautam Sharad Gautam 85 6 6 silver badges 11 11 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science.

Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 1. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. This was helpful. Didn't really expect to get such a fast response from everyone.

Typically on StackOverflow, we thank people with a vote, or by accepting their answer. An answer with many votes 'bubbles to the top of the list' making it easier to find later. If you asked the question, there should also be the outline of a 'tick' character near the number. Click that once to indicate a 'correct' answer.

Shouldn't you close the Scanner? Show 5 more comments. LapinoLapidus 13 5 5 bronze badges. I guess this will read the complete file in memory, whereas the solution of Amir will not do this? Amir Amiri Amir Amiri 1 1 gold badge 8 8 silver badges 12 12 bronze badges. I'm getting this error The method foreach System. Nasser Have you set your jdk to 1. I think it's the matter of java version because this feature works on java 1. This require the pattern to found on the same line.

This might not be the case depending on a lot of factors. Rion Williams Rion Williams Let me try that. I'll get back to you. This will find "Mark Sagal" in Student. Assuming Student. BufferedReader; import java. FileReader; import java. Mark Mark 1 1 gold badge 5 5 silver badges 15 15 bronze badges. Pete Pete I have also a list of rows obtained from a table in mysql. What i'm trying to do is to check if all the words in a every row are valid english words, that's why I compare each row with the words contained in my file.

You can use the method List containsAll Collection. You can read words. The time was O n. The reason your code doesn't work is that occurancy can never be anything other than 0 or 1.

You can see that by following the logic or going through a debugger. If your words. Also, you only ever need to call toLowerCase once, instead of every time you compare. However, be careful with locales. The following code should work as long as you haven't got any non-English characters such as a German eszett or a Greek sigma. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. Asked 4 years, 5 months ago.



0コメント

  • 1000 / 1000