Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Searching for words in a file

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Searching for words in a file

    Good morning , i am writing a program that will wait for the user to enter a molecule , lets say he enters " h20" the program will go to a file search for the word and puts the weight name .. into an array , the problem is that i can't find a way on how to search for the "key word" or in our case "h20" , i want my program to search for this word an puts all of the line after h20 as an array . I hope you can help me guys thanks.
    P.S : I just want a solution on how to find the key word and put the line after the key word in an array , because it may sound i need someone to do the program for me , i needed to clarify that , thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Searching for words in a file

    how to search for the "key word"
    What have you tried?
    One approach: read each line of the file as a String and use one of the String class's methods to see if the line contains the word.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Searching for words in a file

    " one of the String class's "
    could you give me an example ? and are you sure it's the best way that is to divide the lines in lines ? Consider the file is too large isn't it wrong to consider the file as many lines and then to search our word?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Searching for words in a file

    See the String class's API doc for all its methods. For example, look at indexOf().
    Java Platform SE 7
    divide the lines in lines
    Not sure what that means. I'm assuming a line is a string of characters ending with a line-end character.
    The size of the file doesn't matter when reading lines from the file as long as the length of the lines is reasonable. The program will only save some of the file's lines as needed. Most can be discarded after searching them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Searching a word in a compressed file
    By Insence8 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: December 4th, 2013, 04:45 PM
  2. Searching a word in a compressed file
    By Insence8 in forum Object Oriented Programming
    Replies: 0
    Last Post: December 3rd, 2013, 10:38 AM
  3. [SOLVED] Searching for a word in file
    By justyStepi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2013, 08:39 AM
  4. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 10:11 AM
  5. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM