Results 1 to 7 of 7

Thread: text file compare not working with split string

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    15

    text file compare not working with split string

    hi guys could someone explain to me why this does not work.

    I basically have to text files called Books and NewBooks...

    The text files are populated from a web request and the info is then parsed into the text files...when I start the program Books and new books are identical and pretty much a copy of each other.

    more web requests are done to update the NewBooks text file and when I compare them if there is a line in NewBooks that is not in Books it adds that line to a third text file called myNewBooks. Now my initial code that I will show here works as I expected

    Code:
    Dim InitialBooks = File.ReadAllLines("Books.json")
        Dim TW As System.IO.TextWriter
        'Create a Text file and load it into the TextWriter 
        TW = System.IO.File.CreateText("myNewBooks.JSON")
    
        Dim NewBooks = String.Empty
        Using reader = New StreamReader("NewBooks.json")
            Do Until reader.EndOfStream
                Dim current = reader.ReadLine
                If Not InitialBooks.Contains(current) Then 
                    NewBooks = current & Environment.NewLine
    
                    TW.WriteLine(NewBooks)
                    TW.Flush()
                    'Close the File 
                End If
            Loop
        End Using
        TW.Close() : TW.Dispose()
    but because part of the string in my text file lines contain a url which sometimes I find the same book with a different url... I was getting duplicate entries of books becuase the url was the only difference. So I thought that I would split the string before the url so that I just compare the title and description and region ...fyi a line in my text files look similar to this:

    { "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url": "My Url Here", "Image": "My Image Here" };

    So a fellow today helped me figure out how to split my line so it looks more like this:

    { "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url"

    which is great but now when I compare it does not see that the first line contains the split line and I don't understand why... here is the code after it was modified.

    Code:
    Dim InitialBooks = File.ReadAllLines("Books.json")
        Dim TW As System.IO.TextWriter
        'Create a Text file and load it into the TextWriter 
        TW = System.IO.File.CreateText("myNewBooks.JSON")
    
        Dim NewBooks = String.Empty
        Using reader = New StreamReader("NewBooks.json")
            Do Until reader.EndOfStream
                Dim current = reader.ReadLine
                Dim splitAt As String = """Url"""
                Dim index As Integer = current.IndexOf(splitAt)
                Dim output As String = current.Substring(0, index + splitAt.Length)
                If Not InitialBooks.Contains(output) Then 
                    NewBooks = current & Environment.NewLine
    
                    TW.WriteLine(NewBooks)
                    TW.Flush()
                    'Close the File 
                End If
            Loop
        End Using
        TW.Close() : TW.Dispose()
    Your wisdom would be appreciated!!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: text file compare not working with split string

    The .Contains method of an Array or List loops the items to see if there is one that is an exact match, and returns true if there is.

    That failed when the URLs were different, and it also fails when you pass in part of the text (because it cannot be equal to a full item).

    Due to the URL issue you do want to check for part of the text, but you can't use the .Contains method of the array for that, instead the easy to understand way would be to write your own loop to check the items (using the .StartsWith method of the item). Use a boolean variable which you set before the loop (to indicate 'not found'), and if you find a matching item change the value of the boolean (to indicate 'found'). You can then change your If statement to check the boolean variable.

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: text file compare not working with split string

    Just an idea, have you considered defining a class for a book and deserializing the json into instances of this class? That way you could directly compare properties rather than relying on string comparisons.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    15

    Re: text file compare not working with split string

    Quote Originally Posted by si_the_geek View Post
    The .Contains method of an Array or List loops the items to see if there is one that is an exact match, and returns true if there is.

    That failed when the URLs were different, and it also fails when you pass in part of the text (because it cannot be equal to a full item).

    Due to the URL issue you do want to check for part of the text, but you can't use the .Contains method of the array for that, instead the easy to understand way would be to write your own loop to check the items (using the .StartsWith method of the item). Use a boolean variable which you set before the loop (to indicate 'not found'), and if you find a matching item change the value of the boolean (to indicate 'found'). You can then change your If statement to check the boolean variable.
    Thank you will look into this it doesn't seem like I can use the startswith method directly on the string I created from reading all the line of the txt file but maybe I can split it into an array somehow...thanks for the insight.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: text file compare not working with split string

    You don't need to split InitialBooks into an array, because it already is one.

    The return value of File.ReadAllLines is: "A string array containing all lines of the file."
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    15

    Re: text file compare not working with split string

    hey there again ty for your help earlier in this post...I did manage to get that part worked out...but now following the same logic I keep getting an Exception error that string cannot be converted to integer and not quite sure why...Ive tried adding the toString() method to many different places to try and resolve and nothing has worked...can you please just glance over this and see where I am being dumb. The files are still in the same format as stated in my Initial question...and basically trying to see when a new item is found to see if it starts with all the lines in History split it at the word """URL"""

    I commented just above the if statement that throws the exception.

    the exact error is:

    System.InvalidCastException: 'Conversion from string "{ "Title": "Item 1" to type 'Integer' is not valid.'

    Inner Exception
    FormatException: Input string was not in a correct format.


    Code:
    Dim History = File.ReadAllLines("history.json")
    Dim NewestItems = File.ReadAllLines("items.json")
    Dim InitialItems = File.ReadAllLines("Init-items.json")
    
    For x As Integer = 0 To InitialItems.Length - 1
                Dim current As String = InitialItems(0)
                Dim splitAt As String = """Url"""
                Dim index As Integer = current.IndexOf(splitAt)
                Dim output As String = current.Substring(0, index + splitAt.Length)
                Dim recurringItemFound = 0
                If Not NewestItems(x).StartsWith(output) And Not History.Contains(NewestItems(x)) Then
    
                    If cbxRecurringItemsOff.Checked = True Then
                        For Each RecurringItem In History
                            Dim indexHistory As Integer = NewestItems(x).IndexOf(splitAt)
                            Dim outputHistory As String = NewestItemss(x).Substring(0, indexHistory + splitAt.Length)
                            '********the below if statement throws an Exception error*********
                            If NewestItems(RecurringItem).StartsWith(outputHistory) Then
                                recurringItemFound = 1
                            End If
                        Next
                    End If
    Next
    Last edited by bOywOnder123; Jul 3rd, 2018 at 11:15 PM.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: text file compare not working with split string

    NewestItems is an array (the lines of the file "items.json"), and to refer to the elements of an array you put an integer (number without decimal places) in brackets after the array name, eg:
    Code:
    myVariable =  NewestItems(3)
    ...will store the fourth item in the array to a variable (it is the fourth item because counting starts at 0).

    If you want to, you can use a variable instead of a fixed number, eg:
    Code:
    myInteger = 3
    myVariable =  NewestItems(myInteger)
    ...which will return the same as before, because the variable contains the same number.

    In this case you are using NewestItems(RecurringItem) , but is the variable RecurringItem numeric? No it isn't, it is one of the items from the History array (which is also made from the lines from a file, and therefore is a String - as the error message shows it doesn't contain just a number).

    I am not sure, but I'm guessing that instead of NewestItems(RecurringItem) you intended to use just RecurringItem


    There are some other things about your code that seem odd, such as:
    Code:
    For x As Integer = 0 To InitialItems.Length - 1
                Dim current As String = InitialItems(0)
    ...did you mean InitialItems(x) ?
    Code:
                            Dim indexHistory As Integer = NewestItems(x).IndexOf(splitAt)
                            Dim outputHistory As String = NewestItemss(x).Substring(0, indexHistory + splitAt.Length)
    ...are those two different variables, or is one of them a typo? (if it is a typo, let Visual Studio automatically tell you about that by adding Option Explicit to the top of the code file)

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width