Read in text file with Visual Basic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rogerc
    New Member
    • Nov 2005
    • 4

    Read in text file with Visual Basic

    My name is Roger. I am very, very new to programming.
    I hope someone can help me with a problem in VB.
    I need to copy a text file into an array, the file can very in size, but the strings are always the same the same size, separeted by commas.
    Could someone help, please
    Roger
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    Do you have any code that you have already started on? If so paste here, if not let us know so we can point you to some helpful resources.
    niheel @ bytes

    Comment

    • rogerc
      New Member
      • Nov 2005
      • 4

      #3
      no, I do not have any code yet.
      Thank You

      Comment

      • Niheel
        Recognized Expert Moderator Top Contributor
        • Jul 2005
        • 2432

        #4
        Roger,

        Read through this tutorial carefully. Try to put together code that will do what you need after reading it. Test it out to see if it works. If you can't get it to work, paste your code in here and we will help you debug it.

        The page gives you C# and VB version of the code, make sure you are looking at the VB version, to make sure click on the light-grey VB box undear each code sample.

        Visual Basic - Read in text file
        niheel @ bytes

        Comment

        • rogerc
          New Member
          • Nov 2005
          • 4

          #5
          Thank you for the page you suggested. I understood the code but I still cannot read a text file.

          The following is the code I wrote, at the end is the file I need to read.
          I could not send you all the code.

          I thank You very much for any help you can give me.
          roger

          Code:
           ******* 
          Imports System
          Imports System.IO
          Public Class Form1
          Inherits System.Windows.Forms.Form
           
          Dim yes As Integer
          Dim no As Integer
          Dim str As String
           
           
          Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          yes = 0
          no = 0
          Try
          Dim din As StreamReader = File.OpenText("my-weather.nominal.txt")
          Dim myfile As ArrayList = New ArrayList()
          Do
          If "sunny" Then
          LBox1.Items.Add("it is sunny1")
          If "hot" Then
          LBox1.Items.Add("it is hot1")
          If "high" Then
          LBox1.Items.Add("humidity is high")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("Play is TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          Else
          If "normal" Then
          LBox1.Items.Add("humidity is normal")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("Play is TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          End If
          End If
          Else
          If "mild" Then
          LBox1.Items.Add("it is mild")
          If "high" Then
          LBox1.Items.Add("humidity is high")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          Else
          If "normal" Then
          LBox1.Items.Add("humidity is normal")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          End If
          End If
          Else
          If "cool" Then
          LBox1.Items.Add("it is cool")
          If "high" Then
          LBox1.Items.Add("humidity is high")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("Play is TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          Else
          If "normal" Then
          LBox1.Items.Add("humidity is normal")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("Play is TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          End If
          End If
          End If
          End If
          End If
          Else
          If "rainy" Then
          LBox1.Items.Add("it is rainy")
          If "hot" Then
          LBox1.Items.Add("it is hot")
          If "high" Then
          LBox1.Items.Add("humidity is high")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("Play is TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          Else
          If "normal" Then
          LBox1.Items.Add("humidity is normal")
          If "True" Then
          LBox1.Items.Add("it is windy")
          LBox1.Items.Add("Play is TRUE")
          yes = yes + 1
          LBox1.Items.Add(yes)
          Else
          LBox1.Items.Add("it is not windy")
          LBox1.Items.Add("Play is FALSE")
          no = no + 1
          LBox1.Items.Add(no)
          End If
          End If
          End If
          Else
          If "mild" Then
          LBox1.Items.Add("it is mild")
          If "high" Then
          LBox1.Items.Add("humidity is high")
          End If
          End If
          End If
          Loop Until str = Nothing
          Catch
          Console.WriteLine("no good")
          End Try
          End Sub
           
          Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
          Close()
           
          End Sub
           
          Private Sub LBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LBox1.SelectedIndexChanged
           
          End Sub
          End Class
           
          *********
          @relation weather.symbolic
           
          @attribute outlook {sunny, overcast, rainy}
          @attribute temperature {hot, mild, cool}
          @attribute humidity {high, normal}
          @attribute windy {TRUE, FALSE}
          @attribute play {yes, no}
           
          @data
          sunny,hot,high,FALSE,no
          sunny,hot,high,TRUE,yes
          overcast,hot,high,FALSE,no
          rainy,mild,high,FALSE,no
          rainy,cool,normal,FALSE,no
          rainy,cool,normal,TRUE,no
          overcast,cool,normal,TRUE,yes
          sunny,mild,high,FALSE,yes
          sunny,cool,normal,FALSE,yes
          rainy,mild,normal,FALSE,no
          sunny,mild,normal,TRUE,yes
          overcast,mild,high,TRUE,yes
          overcast,hot,normal,FALSE,no
          rainy,mild,high,TRUE,no
          Last edited by Niheel; Nov 22 '05, 04:57 AM.

          Comment

          • Niheel
            Recognized Expert Moderator Top Contributor
            • Jul 2005
            • 2432

            #6
            You should always place code in the code tags. I did that for you this time.

            I will take a look at the code later tonite, hopefully i can help you out. My VB is very limited.
            niheel @ bytes

            Comment

            Working...