• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String reading

 
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A file containing:
18 Pelobacter
19 Pelobacter carbinolicus
20 Phenylobacterium
21 Phenylobacterium immobile
22 Shewanella
23 Shewanella colwelliana
24 Shewanella putrefaciens
22 putrefaciens
3422 colwelliana

The coding to search for a string:
for example 22




this is displaying:

22 Shewanella
22 putrefaciens
3422 colwelliana // should not have been displayed since its 3422 not 22

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your code is looking for the string "22" in the line -- so it should match and be displayed.

If you don't want to match the string "3422", you will need to handle these cases too.

Henry
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But "3422" does contain "22".

There is something wrong with the way you are reading those bacteria names and associating them with lines. Do you have a Bacterium class? Can you create a List<Bacterium> and read the lines and convert them to a Bacterium?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at how String#split(regex, index) can help you solve this.
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no i dont have any classes
i am just reading from a file
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you just want lines containing exactly "22" then you need to either search for "22" plus the character either side of it or convert the whole number to an int and check it == 22.
How you do either of these options depends on the format of your file eg does the line start with the number or can it be embedded in a sentence? Is the file guaranteed to contain only ASCII characters? etc.

For example: If the number is always at the beginning of the line and is always followed by a " " character then you could find the index of "22 " and test to see if it is 0. If there maybe white space before the number then trim() the line first.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:For example: If the number is always at the beginning of the line and is always followed by a " " character then you could find the index of "22 " and test to see if it is 0.



Or use the String.startsWith() method instead of the String.indexOf() method...
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the numbers always starts at beginning of file
and i have remove the spaces
then comes the name
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now vals[0] contains the number (as a string), and vals[1] contains the rest of the line after the initial space.
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println("Searching for " + stringSearch + " in file...");

while (( line = bf.readLine()) != null)
{
// linecount++;
// int indexfound = line.indexOf(stringSearch);
String[] vals = line.split(" ",2);
if (vals[0]==stringSearch)
{
System.out.println(vals[0]+"\t"+vals[1]);
}
}
bf.close();

 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is not working the vals[] method
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't compare strings using ==. Use
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it workssss
thankss
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the while-loop, add this:

to see what line really looks like. And is there really a space, or is it a tab? You can split on any whitespace by using "\\s+" as the delimiter string in the split() call.
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it puts the line in square brackets
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry - my last post was in response to your comment about it not working.
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it works with both codes (y)
thanks a lot
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vani Sweety wrote:no i dont have any classes
i am just reading from a file


Yes, but that file must contain something of interest; and it's a darn good idea to convert "things of interest" into objects. But to do that you need to define a class for them.

Question: How big is that file? Kilobytes? Megabytes? Gigabytes? If it's anything less than a million lines or so (assuming you're on a proper machine and not a Smartphone), it may well be worth reading the whole thing into a List or Map of some sort, because then you can re-read it as many times as you like, and it'll be as fast as it can be.

Winston
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the file is of 115 MB
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vani Sweety wrote:the file is of 115 MB


Ah, well that might be a bit too big. It seems to me you were very close with your original code; you just weren't including the "separator" character (called a 'delimiter'). A slightly more flexible alternative is to use a regular expression, viz:The
  Pattern stringSearch = Pattern.compile("22\\s");
creates a regular expression of "2" + "2" + any whitespace character, and the lookingAt() call matches it ONLY at the start of the line; which makes the matching process as fast as possible.

And the great advantage of doing it that way is that it translates very nicely to the version 8 style mentioned above, viz:although it's perfectly possible to do the same thing with a standard "matcher" method like startsWith().

I strongly urge you to look at regular expressions though - and especially the Pattern and Matcher classes - because they're extremely useful to know about.
Some methods in String, including split(), use them as well; so you'll have to read up on them eventually anyway.

One other point: Did you notice the
  throw new IOError(e);
line? What it does is to re-throw the IOException as a runtime exception, which doesn't need to be checked for.

You should never "swallow" exceptions, even if they're checked ones like IOException (which can admittedly be a pain) because you're depriving anyone who runs your code of valuable information if something goes wrong - namely, the stacktrace.

HIH

Winston
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





if i use this method the search will give me only result id of 22
and if i would like the user to input the searching value which might be any value how would i implement that
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vani Sweety wrote:and if i would like the user to input the searching value which might be any value how would i implement that


You really should look at the Pattern class, and probably some more about regular expressions in general. This site is pretty good.

However, it's unlikely that you want your user to search for any value, because that will return the entire 110 megabytes.

So: what do you actually want them to be able to search for? Any single value?

Winston
 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would like that the user can input any id they want
and according that id they can view which names matches that id
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vani Sweety wrote:i would like that the user can input any id they want


OK, well there's more than one way to do that.
1. You can supply it as argument in your java command when you run the program - that's what the 'String[] args' parameter is for.
2. You can request it from the user. You plainly know how to use System.out; do you know how to use System.in? Because that's usually the keyboard.

Winston
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I missed something, this thread seems to have gotten off on a tangent about regular expressions.
So, to clarify:

User's requested id:
22

Data in file:

If this is the case then regular expressions are overkill and equals() will suffice. Of course this depends on splitting the line on the first occurrence of a white space character ("\\s+").

Correct me if I'm wrong.

IF you really want a user to be able to match patterns, then wild cards are more user friendly then regular expressions. Example "*22", or "*22*". Wild cards can easily be converted to regular expressions in a method with a small number of lines. I can give you a method to do this if the moderators don't object.

 
Vani Sweety
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Unless I missed something, this thread seems to have gotten off on a tangent about regular expressions.
So, to clarify:

User's requested id:
22

Data in file:






it works corectly

but if there is an alternative method to do it as you mentioned
i would like to learn it
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Unless I missed something, this thread seems to have gotten off on a tangent about regular expressions.


Because they're rather hard to get away from. You use one yourself in your split() example.

If this is the case then regular expressions are overkill and equals() will suffice.
Of course this depends on splitting the line...


Which requires a regular expression.

Actually, I don't think you need to split() at all. If the user requests an ID, why not just put it in a variable called 'Id' and then call:
  line.startsWith(Id + " ");

That of course relies on:
(a) the data in the file being separated by spaces.
(b) "Id" being the first "field" in the line.
but if that's actually the case, then it should work just fine, and it'll probably be as fast as you can make it.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vani Sweety wrote:but if there is an alternative method to do it as you mentioned i would like to learn it


Well, as I said above, you could do something like:
which saves you the split(), which is a moderately heavyweight function in this context.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic