• 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

Searching a file

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to search a txt file for a specific keyword and then output all the lines that contain that keyword.
Right now I I think I have my search done but I don't know how I would print the whole line.



Right now it doesnt even let me enter in any values for the search. Not sure what I've done wrong..

Thanks for the help.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Right now I I think I have my search done but I don't know how I would print the whole line. ... Right now it doesnt even let me enter in any values for the search. Not sure what I've done wrong.


That's hard to say, as we don't know how that TextIO class works (it is not part of the JRE). I would assume that a method called "getln" reads an entire line of input, so that "word" would actually contain a line of input text, not just a single word. If it does, you can use System.out.println to print it to the console. On the other hand, if TextIO.getln reads from a file, then line 6 wouldn't work for reading the search word from the console. But that's just speculation, absent any knowledge of TextIO.

If you want to use a more standard approach, read https://coderanch.com/how-to/java/UserInput for how to get input from the console, and http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/ for how to read a file line by line.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TextIO.getlnWord(); // Reads one "word" as a value of type String.
TextIO.getln(); // Reads an entire input line as a String.

I guess my search should probably be just getlnWord. At the moment though the program starts the while loop before I even get to insert my search. I don't understand why this is as I thought the program should go top down... seems it just doesn't want to wait for a user input.

TextIO is used by some schools to avoid using scanner I guess. I guess it is suppose to be a lot easier but I think that conversation is a bit over my head still.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that class read both from the console and from a file using the exact same methods? That seems unlikely. Line 6 should read from the console whereas line 8 should read from the file (the contents of which have presumably been read into memory in line 1), no? It would seem natural that there needs to be some distinction between the two method invocations to indicate the different sources of where to read from.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the same but the professor has examples posted that use these for line reading and user input:
Reads a file and outputs average word length (I've just included the loop with the getln):


And part of another program that checks for a palindrom:


 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this the class http://math.hws.edu/javanotes6/source/TextIO.java ? If so, it does not look to me like you can use it to read from two sources at the same time. You can apparently do both in the same app if calling the proper methods (like readFile and readStandardInput) to switch between two sources, though. From a cursory look at the code it seems the easiest way to do so would be to move line 1 of your code after line 6, so that the calls using the two input sources are not interspersed.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is! Well, that at least let me write my search keyword. Thanks, I understand what you're saying now. Next, I just need to figure out why it doesn't find/doesn't print afterwards. I'll keep working on it tomorrow I guess. Pretty late here now. I wonder if I can't use the ignorecase as I have. Seems most people are using tolowercase.

This is what I have now:

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can print out "search" and "word" to the console to make sure they are what you think they are.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I just checked that and both variables are working. Printing 'word' is giving me the entire contents of the file and 'search' is what it should be. It must be the comparison then I think.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it to work completely except for formatting. My output includes the spaces of the text file so for instance I'm printing:

1- Hello
2- World!

But I'm suppose to print:
1-Hello
2-World!

Any hints as to how I could remove the spaces?

Figured it out. Help files are helpful.

word=word.replaceAll("^\\s+", "");
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
word.trim() would be another solution.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nevermind.
TRIM(). I should have just listened to you to begin with on that one.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic