Results 1 to 10 of 10
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Read text file and compare strings

    I have a text file which has a link on every line. I want to write a method that will take a string and compare it to all the links in the text file to see if an identical link exists in the text file.

    If i can accomplish this I will be one very happy bunny. Can someone please help me to just write something up that roughly does this. A rough draft will more than suffice, I will perfect the concept.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Here is some pseudocode
    Java Code:
    create arraylist of strings
    open streams for file reading
    loop
      add line to array list
    end loop
    
    get input
    loop through arraylist
      see if item contains input
    end loop
    You can also split the strings in the array list up with the split method and store everything in an array lists.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by africanhacker View Post
    I have a text file which has a link on every line. I want to write a method that will take a string and compare it to all the links in the text file to see if an identical link exists in the text file.

    If i can accomplish this I will be one very happy bunny. Can someone please help me to just write something up that roughly does this. A rough draft will more than suffice, I will perfect the concept.
    Java Code:
    String line;
    String link = "your String to search for";
    File file = new File("PATH");
    BufferedReader bReader = new BufferedReader(file);
    
    while((line=bReader.readLine()) != null){
    if(line.equals(link)){
    System.out.println(link+" identical match found in the text file!");
    }
    }

  4. #4
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    @solarsonic

    Thanks for that code. The only problem here is that the list of links is an arraylist. How would I manage to update String link dynamically such that after searching for the first I go to the next? At the same time the while looping will be running through each line.

    Basically I want a way for the while loop to start of by taking index 0 of arraylist, use that for the value of link in the first iteration. After this it should move to index 1 and so on.

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by africanhacker View Post
    @solarsonic

    Thanks for that code. The only problem here is that the list of links is an arraylist. How would I manage to update String link dynamically such that after searching for the first I go to the next? At the same time the while looping will be running through each line.

    Basically I want a way for the while loop to start of by taking index 0 of arraylist, use that for the value of link in the first iteration. After this it should move to index 1 and so on.
    Java Code:
    ArrayList links;
    
    while((line=bReader.readLine()) != null){
    for(String s : links){
    if(line.equals(s)){
    System.out.println(s+" identical match found in the text file!");
    break; // breaks the enhanced for loop to search the next line
    }
    }
    }
    This will look through every line and check if it is equal to any value in the ArrayList.

  6. #6
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks man. Your code works!

    When you guys do this off the top off your heads I am always impressed. How long have you been programming?
    Last edited by africanhacker; 04-01-2011 at 01:26 AM.

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by africanhacker View Post
    Thanks man. Your code works!

    When you guys do this off the top off your heads I am always impressed. How long have you been programming?
    No problem :) I've been programming for years, but I haven't gotten intermediate+ level until recently. I'm not nearly as good as some others here...

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Neither am I, I actually only have 4-5 months experience with java, I just see a lot of similar questions popping up on the forums, and I have been reading a lot. In time it gets easier to solve a lot of problems.

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by sunde887 View Post
    Neither am I, I actually only have 4-5 months experience with java, I just see a lot of similar questions popping up on the forums, and I have been reading a lot. In time it gets easier to solve a lot of problems.
    Very similar to me :)

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    This can be better achieved by using a Set as they do not allow duplicates. Simply read and add each String to the Set. If it returns false then that String must be already in the Set and therefore a duplicate.

Similar Threads

  1. Problem writing multiple strings to a text file
    By Yogesh_P in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 01:58 AM
  2. Replies: 2
    Last Post: 01-26-2009, 05:28 PM
  3. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM
  4. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 04:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •