none
Search a text document for a certain number RRS feed

  • Question

  • Hello everyone

    I am using Visual Studio for a project I'm doing this summer.

    The user will enter their CID number into a textbox, and when they click a connect button, the program will search ONE of FOUR (Has to be random, rules for using the API) text documents for their callsign.

    Here is my current code:

    Public Class Form1
        Private Sub Connect_Click(sender As Object, e As EventArgs) Handles Connect.Click 'Connect function
            If Connect.Text = "Connect" And IsNumeric(CIDBox.Text) = True Then 'Checks if it's numbers in the box
                Dim chosenURL = myURLs(myRand.Next(0, 4)) 'Pulls from random URL
                Connect.Text = "Disconnect"
                callsignbox.Text = "UsersCallsign" 'THIS NEEDS TO BE PULLED FROM ONE OF THE URLS
                CurrentAirspace.Text = "Gander FIR - Offline"
            ElseIf Trim(CIDBox.Text & vbNullString) = vbNullString Then 'Checks if CID box is empty then shows instructions
                Instructions.Visible = True
            ElseIf IsNumeric(CIDBox.Text) = False Then 'Checks if CID box has letters then shows instructions
                NumberInfo.Visible = True
            ElseIf Connect.Text = "Disconnect" Then 'Disconnects them
                Connect.Text = "Connect"
                callsignbox.Text = vbNullString
            End If
        End Sub
        Private myURLs As String() = {"http://vatsim-data.hardern.net/vatsim-data.txt", "http://wazzup.flightoperationssystem.com/vatsim/vatsim-data.txt", "http://vatsim.aircharts.org/vatsim-data.txt", "http://info.vroute.net/vatsim-data.txt"}
        Private myRand As New Random
    End Class

    I have the 4 text documents as an array, and a random object, but I'm not sure how to go about pulling their callsign from the text files. Could anyone help me out? Thanks

    Monday, May 28, 2018 7:50 PM

Answers

  • Well I did a live test according to Viorel_'s recommendation and used this URL http://vatsim.aircharts.org/vatsim-data.txt for CID 811479 of Mark Crabtree. I commented the code below on basically how it works. However if the websites alter the format of their text document the code will not work correctly.

    The code also assumes the CID will not be present as another number or part of a number anywhere else in a line which is not a good idea really. It may actually be better to split each line and check the index of the split lines where the CID resides to validate that it is the actual CID and not just some other number or part of some other number in a line.

    I still do not understand what the Random is going to be used for in your code though as their is no complete reason of what it's purpose is. And since you are new to this stuff you should use the MSDN Library Search Engine to search for information that you do not understand probably.

    It would behoove you to learn string methods to understand how they work as well to write the code so that it will work surely and not assuming anything.

    Also I looked in the Ascii Table to try to determine what character to split the entire string retrieved from the website with and got lucky with character 13, carriage return. It could be character 10, New Line, would've worked.

    I don't know your level of knowledge so am not sure you will understand this code or not.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            TextBox1.Text = "811479" ' CID of Mark Crabtree
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            ' Donwload file from URL to string
    
            Dim text = New Net.WebClient().DownloadString("http://vatsim.aircharts.org/vatsim-data.txt")
    
            ' Clear TextBox's
    
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()
            TextBox6.Clear()
            TextBox7.Clear()
            TextBox8.Clear()
    
    
            If text <> "" Then ' Validate string contains data
    
                ' Split the string on the carriage retruns (ChrW(13)). This will provide a string array with each line of the
                ' text so that each line can be read for the CID. Test was for the CID used by Mark Crabtree - 811479.
    
                Dim SplitText() As String = text.Split(ChrW(13))
                Dim InputString As String = ""
                For i = 0 To SplitText.Count - 1
                    If SplitText(i).Contains(TextBox1.Text) Then ' Check for CID displayed in TextBox1.
                        InputString = SplitText(i) ' Get the index to a string if it has the CID.
                        Exit For
                    End If
                Next
    
                ' The array for this test had 768 indexes of lines from the downloaded string.
    
                ' Validate the InputString is not empty. Then split it on colons :.
                ' Note that the values are contained in indexes 0 to 6 of the OutputString. However index 2 has the name
                ' and home airport of the pilot. So you have to get the substring of the name and the substring of the home airport
                ' and the code assumes the home airport is always the last 4 characters of this indexes string.
    
    
                Dim OutputString() As String
                If InputString <> "" Then
                    RichTextBox1.AppendText(InputString)
                    OutputString = InputString.Split(":"c)
                    TextBox2.Text = OutputString(0)
                    TextBox3.Text = OutputString(1)
                    TextBox4.Text = OutputString(2).Substring(0, OutputString(2).Count - 4)
                    TextBox5.Text = OutputString(2).Substring(OutputString(2).Count - 4, 4)
                    TextBox6.Text = OutputString(3)
                    TextBox7.Text = OutputString(5)
                    TextBox8.Text = OutputString(6)
                End If
            End If
    
        End Sub
    
    End Class


    La vida loca

    • Edited by Mr. Monkeyboy Monday, May 28, 2018 11:42 PM
    • Marked as answer by GrantMeder Tuesday, May 29, 2018 1:37 AM
    Monday, May 28, 2018 11:11 PM

All replies

  • In order to download the whole file, try this:

       Dim text = New Net.WebClient().DownloadString(chosenURL)

     

    Then you can show it in textbox. But if you want to extract a specific portion, then give details.


    Monday, May 28, 2018 8:06 PM
  • In order to download the whole file, try this:

       Dim text = New Net.WebClient().DownloadString(chosenURL)

     

    Then you can show it in textbox. But if you want to extract a specific portion, then give details.


    I can't download the text file because they update every 2 minutes...let me explain.

    The text files have a long list of all the active users on the network. Each user has a long line of information. For example, I'm currently on the network right now. My string looks like this:

    VIR12E:1297722:Grant Meder KHSV:PILOT::50.70921:-31.82075:218:198:H/B789/L:419:KBOS:17000:EGLL:USA-C:100:1:1303:::2:I:1710:1710:6:15:7:16:EGLF:+VFPS+/V/PBN/A1B1C1D1L1O1S1 DOF/180528 REG/N821AN EET/CZQM0037 CZQX0129 47N050W0207 49N040W0259 EGGX0348 52N020W0435 EISN0458 LIMRI0458 XETBO0502 BANBA0538 EGTT0538 RVR/75 RALT/CYYT BIKF RMK/TCAS:LBSTA6 LBSTA V139 ENE/N0488F370 Q822 ALLEX N149D PORTI NATT 47N050W/M085F380 NATT 51N030W/M085F390 NATT XETBO DCT BANBA/N0488F390 UP2 OKESI P2 BEDEK:0:0:0:0:::20180528164311:85:30.097:1019:

    The underlined part is all the information I need to pull. Let me break it down:"

    VIR12E:1297722:Grant Meder - Airplane Callsign : CID (Network ID) : Name

    KHSV:PILOT: - Home Airport : Rank

    :50.70921:-31.82075: Latitude : Longitude

    Those are all the components I need. The user will enter his/her CID (The number). My program will search for them in one of the four text files, and will display his/her Airplane Callsign and Lat/Long coordinates. I'm just not sure how to do that.


    • Edited by GrantMeder Monday, May 28, 2018 8:44 PM Fixed formatting
    Monday, May 28, 2018 8:42 PM
  • I went to the first URL and there's alot more data than what you show in your string. So I suspect all 4 URL's are the same way.

    You can't use a Random to get which URL has the correct name in it. You will need to pull the text file from each URL and find the line that contains the data you want by CID somehow. That way you can get a single line from a text file and parse it.

    In my opinion it would be simplest to download each URL's text to a temp file. Then use a stream reader to read each line of the file until the CID is found. Once that line is found parse it and you will know by splitting the line on colons which area in the array contains the information you desire as long as the files are always formatted in this manner. Although you could split the file downloaded using a WebClient also to a string.

    Below image is part of what I saw from the first URL.


    La vida loca

    Monday, May 28, 2018 9:50 PM
  • I can't download the text file because they update every 2 minutes.

    Then you will have to download a new file every two minutes.   Unless they provide some other means of examining the data - what is the API you mention?

    The underlined part is all the information I need to pull.

    The first thing to do is to get the whole file and examine it in detail as byte data, not text.  You need to find out the rule that identifies the data you need.   For instance, that it is numeric and exactly 7 digits long.  Or, more likely, that there is other data in the file such as a CR or CR/LF that marks the beginning of a record, and the ID is the 2nd.    If that isn't available then you will need to scan the whole file for an exact match.  See:
    https://msdn.microsoft.com/en-us/library/system.string.split(v=vs.110).aspx

    Monday, May 28, 2018 10:05 PM
  • I went to the first URL and there's alot more data than what you show in your string. So I suspect all 4 URL's are the same way.

    You can't use a Random to get which URL has the correct name in it. You will need to pull the text file from each URL and find the line that contains the data you want by CID somehow. That way you can get a single line from a text file and parse it.

    In my opinion it would be simplest to download each URL's text to a temp file. Then use a stream reader to read each line of the file until the CID is found. Once that line is found parse it and you will know by splitting the line on colons which area in the array contains the information you desire as long as the files are always formatted in this manner.

    Below image is part of what I saw from the first URL.


    Yes, you are correct. The long string I attached earlier was just my user information. Those websites have every users information.

    The network requires I pull from a random website, which is why I can't just use one. 

    So, if I need to I can download the files every 2 minutes - I have good internet so it would be no problem. How would I go about doing that?

    How would I set up the stream reader to search for JUST the callsign? And how would I tell the program to put their callsign in the CallsignBox.text?

    Could you always explain (or link :P) on how to parse the data into an array?

    Sorry if this is a lot..I'm brand new to this stuff.

    Monday, May 28, 2018 10:52 PM
  • Well I did a live test according to Viorel_'s recommendation and used this URL http://vatsim.aircharts.org/vatsim-data.txt for CID 811479 of Mark Crabtree. I commented the code below on basically how it works. However if the websites alter the format of their text document the code will not work correctly.

    The code also assumes the CID will not be present as another number or part of a number anywhere else in a line which is not a good idea really. It may actually be better to split each line and check the index of the split lines where the CID resides to validate that it is the actual CID and not just some other number or part of some other number in a line.

    I still do not understand what the Random is going to be used for in your code though as their is no complete reason of what it's purpose is. And since you are new to this stuff you should use the MSDN Library Search Engine to search for information that you do not understand probably.

    It would behoove you to learn string methods to understand how they work as well to write the code so that it will work surely and not assuming anything.

    Also I looked in the Ascii Table to try to determine what character to split the entire string retrieved from the website with and got lucky with character 13, carriage return. It could be character 10, New Line, would've worked.

    I don't know your level of knowledge so am not sure you will understand this code or not.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            TextBox1.Text = "811479" ' CID of Mark Crabtree
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            ' Donwload file from URL to string
    
            Dim text = New Net.WebClient().DownloadString("http://vatsim.aircharts.org/vatsim-data.txt")
    
            ' Clear TextBox's
    
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()
            TextBox6.Clear()
            TextBox7.Clear()
            TextBox8.Clear()
    
    
            If text <> "" Then ' Validate string contains data
    
                ' Split the string on the carriage retruns (ChrW(13)). This will provide a string array with each line of the
                ' text so that each line can be read for the CID. Test was for the CID used by Mark Crabtree - 811479.
    
                Dim SplitText() As String = text.Split(ChrW(13))
                Dim InputString As String = ""
                For i = 0 To SplitText.Count - 1
                    If SplitText(i).Contains(TextBox1.Text) Then ' Check for CID displayed in TextBox1.
                        InputString = SplitText(i) ' Get the index to a string if it has the CID.
                        Exit For
                    End If
                Next
    
                ' The array for this test had 768 indexes of lines from the downloaded string.
    
                ' Validate the InputString is not empty. Then split it on colons :.
                ' Note that the values are contained in indexes 0 to 6 of the OutputString. However index 2 has the name
                ' and home airport of the pilot. So you have to get the substring of the name and the substring of the home airport
                ' and the code assumes the home airport is always the last 4 characters of this indexes string.
    
    
                Dim OutputString() As String
                If InputString <> "" Then
                    RichTextBox1.AppendText(InputString)
                    OutputString = InputString.Split(":"c)
                    TextBox2.Text = OutputString(0)
                    TextBox3.Text = OutputString(1)
                    TextBox4.Text = OutputString(2).Substring(0, OutputString(2).Count - 4)
                    TextBox5.Text = OutputString(2).Substring(OutputString(2).Count - 4, 4)
                    TextBox6.Text = OutputString(3)
                    TextBox7.Text = OutputString(5)
                    TextBox8.Text = OutputString(6)
                End If
            End If
    
        End Sub
    
    End Class


    La vida loca

    • Edited by Mr. Monkeyboy Monday, May 28, 2018 11:42 PM
    • Marked as answer by GrantMeder Tuesday, May 29, 2018 1:37 AM
    Monday, May 28, 2018 11:11 PM
  • So, if I need to I can download the files every 2 minutes - I have good internet so it would be no problem. How would I go about doing that?

    How would I set up the stream reader to search for JUST the callsign? And how would I tell the program to put their callsign in the CallsignBox.text?

    You will choose a random website by setting the connection strings into a string array, and using a random number to select the array element to use.  See:
    https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx

    The stream reader can't search.  You need to download the file into a string and search the string.

    How you will search depends on what you are searching for.   If the records are delimited somehow (eg, with a CR or CR/LF) you will search for that, breaking the string into multiple separate strings.   Then you examine each of those, perhaps by breaking them at the ':' and examining each separate one, or selecting a particular one if it always falls at the same position.  The point is that you will start from a detailed understanding of the exact format (including possible hidden characters) of the text that you are downloading. Only when you have that can you consider how you might do the searching.

    Tuesday, May 29, 2018 1:33 AM

  • Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            TextBox1.Text = "811479" ' CID of Mark Crabtree
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            ' Donwload file from URL to string
    
            Dim text = New Net.WebClient().DownloadString("http://vatsim.aircharts.org/vatsim-data.txt")
    
            ' Clear TextBox's
    
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()
            TextBox6.Clear()
            TextBox7.Clear()
            TextBox8.Clear()
    
    
            If text <> "" Then ' Validate string contains data
    
                ' Split the string on the carriage retruns (ChrW(13)). This will provide a string array with each line of the
                ' text so that each line can be read for the CID. Test was for the CID used by Mark Crabtree - 811479.
    
                Dim SplitText() As String = text.Split(ChrW(13))
                Dim InputString As String = ""
                For i = 0 To SplitText.Count - 1
                    If SplitText(i).Contains(TextBox1.Text) Then ' Check for CID displayed in TextBox1.
                        InputString = SplitText(i) ' Get the index to a string if it has the CID.
                        Exit For
                    End If
                Next
    
                ' The array for this test had 768 indexes of lines from the downloaded string.
    
                ' Validate the InputString is not empty. Then split it on colons :.
                ' Note that the values are contained in indexes 0 to 6 of the OutputString. However index 2 has the name
                ' and home airport of the pilot. So you have to get the substring of the name and the substring of the home airport
                ' and the code assumes the home airport is always the last 4 characters of this indexes string.
    
    
                Dim OutputString() As String
                If InputString <> "" Then
                    RichTextBox1.AppendText(InputString)
                    OutputString = InputString.Split(":"c)
                    TextBox2.Text = OutputString(0)
                    TextBox3.Text = OutputString(1)
                    TextBox4.Text = OutputString(2).Substring(0, OutputString(2).Count - 4)
                    TextBox5.Text = OutputString(2).Substring(OutputString(2).Count - 4, 4)
                    TextBox6.Text = OutputString(3)
                    TextBox7.Text = OutputString(5)
                    TextBox8.Text = OutputString(6)
                End If
            End If
    
        End Sub
    
    End Class


    This works!!! Thank you so much!! I thought this was going to take me forever to figure out! 

    Tuesday, May 29, 2018 1:38 AM
  • Well it is not what you should use except for test purposes. You should refine it down so that it assumes nothing. As I said the CID number could show up somewhere else farther along in one of the strings (lines) that get split from the main string on carriage return as there happen to be many areas in each string containing numbers. But if you split each string split from the main string first and the CID number is in index 1 of each split string then you can see if that split string contains the CID in index 1.

    So please re-read my post on how it assumes certain things which may not be accurate such as the home airport which for all I know may not always be four letters. Then you would have to take the index 2 and back pedal from the end of the string at index 2 until you find a space to verify that the letters after the space are the home airport or whatever it is.

    I recommend you practice until you become proficient with and have an understanding of how the code and the string methods work so that you can alter the code to be more robust along the actual lines of how it should work not assuming things like it does now.

    After all it's easy to copy and paste code that works but that does not mean the code is robust enough for the purpose at hand or that it shouldn't be refined once you know and understand what it is doing. And why you should alter it here or there or altogether once you have a complete understanding of what it does.


    La vida loca

    Tuesday, May 29, 2018 2:10 AM