1

i have a text file with the following data:

Calculated Concentrations
30.55 73.48 298.25 27.39 40.98 11.21 99.22 33.46 73.99 12.18 30.7 50 28.4 34.33 29.55 70.48 43.09 28.54 50.78 9.68 62.03 63.18 28.4 100 23.83 68.65 10.93 ?????? 31.42 8.16 24.97 8.3 114.97 34.92 15.53 200 32.15 29.98 23.69 ?????? 23.41 33.6 92.03 32.73 13.58 58.44 94.61 400 159.98 18.05 50.94 37.12 15.25 46.75 315.22 69.98 13.58 ?????? 58.77 208.82 11.07 38.15 86.31 35.5 41.88 28.25 5.39 40.83 29.98 54.42 69.48 36.09 13.16 23.26 19.31 147.56 31.86 6.77 19.45 33.6 32.87 205.47 134.21 ?????? 17.35 9.96 58.61 13.44 23.97 22.13 145.17 29.55 26.54 37.12 198.33

and i would like to load this data into an array.

  1. how do i enable the user to open a file of his choosing in vb.net?
  2. how do i read these values into an array in vb.net?

i would like to clarify that what person-b has suggested works very very well. specifically the fields = calculationText.split(" ") was exactly what i needed; however, my next issue is the following. the data above is actually in this format:

http://pastebin.com/d29ae565b

where the i need the first value to be 0, then 50, then 100 etc. and i need it to read columns from left to right. the problem is that the values are not reading into the array in this manner. the values are actually reading like this: 6.65, 84.22, ????, 35.15. please help!

3 Answers 3

4

To read a file into a String variable, in VB.NET, you can use the System.IO.File.ReadAllText() function. An example is:

Imports System.IO '// placed at the top of the file'

'// some code'

Dim calculationText As String
calculationText = File.ReadAllText("calculations.txt") '// gets all the text in the file'

Where "calculations.txt" is the file name.

To your next point - to allow the user to load a file of their choosing, you can use the OpenFileDialog type. An example:

Dim fileName As String
Dim openDlg As OpenFileDialog
openDlg = New OpenFileDialog() '// make a new dialog'
If openDlg.ShowDialog() = DialogResult.OK Then
    '// the user clicked OK'
    fileName = openDlg.FileName '// openDlg.FileName is where it keeps the selected name'
End If

Combining this, we get:

Imports System.IO 

'// other code in the file'

Dim fileName As String
Dim openDlg As OpenFileDialog
openDlg = New OpenFileDialog() '// make a new dialog'
If openDlg.ShowDialog() = DialogResult.OK Then
    '// the user clicked OK'
    fileName = openDlg.FileName
End If

Dim calculationText As String
calculationText = File.ReadAllText(fileName)

Now, we need to process the input. First, we need to make a list of decimal numbers. Next, we put everything in the file that is a number in the list:

Dim numbers As List(Of Decimal)
numbers = New List(Of Decimal)() '// make a new list'

Dim fields() As String
fields = calculationText.Split(" ") '// split all the text by a space.'

For Each field As String in fields '// whats inside here gets run for every thing in fields'
    Dim thisNumber As Decimal
    If Decimal.TryParse(field, thisNumber) Then '// if it is a number'
        numbers.Add(thisNumber) '// then put it into the list'  
    End If
Next

This won't include Calculated Concentrations or ????? in the list. For usable code, just combine the second and last code samples.

EDIT: In response to the comment below.
With List objects, you can index them like this:

Dim myNumber As Decimal
myNumber = numbers(1)

You do not need to use the Item property. Also, when showing it in a message box, you need to turn it into a String type first, like this:

MsgBox(myNumber.ToString())
Sign up to request clarification or add additional context in comments.

16 Comments

sure! well it's not giving an error message, but when i try this code, it gives me a msgbox of "0" all the time For Each field As String In fields Dim thisNumber As Decimal numbers.Add(thisNumber) MsgBox(thisNumber) 'If Decimal.TryParse(field, thisNumber) Then ' MsgBox(thisNumber) ' numbers.Add(thisNumber) ' End If Next
Aah! You need to use the MsgBox after the last "End If". What the If does, is it checks whether it actually is a number or not, and if it is, it puts it there. Currently, you're trying to show it before it's parsed.
i did all your changes and still msgbox is returning 0
For Each field As String In fields Dim thisNumber As Decimal numbers.Add(thisNumber) If Decimal.TryParse(field, thisNumber) Then numbers.Add(thisNumber) End If Next Dim myNumber As Decimal myNumber = numbers(1) MsgBox(myNumber.ToString()) still returning zero
Can you paste your current code onto pastebin.com, then post the URL here. It makes the code easier to read. Sorry about all this.
|
2

Once you have the file coming from an OpenFileDialog control or other UI control. Here's something you could do:

Dim filePath As String = "file.txt" ''* file coming from control
Dim fileContents As String =  System.IO.File.ReadAllText(filePath)
Dim contentArray() As String = fileContents.Split(" ")

Then you can iterate through the array and TryParse to a number as needed.

Comments

2
  1. OpenFileDialog
  2. Read through character by character, looking for spaces to mark the end of a number, then parse the number an add it to a List. Or if the file is always short, you could use StreamReader.ReadToEnd and String.Split.

Code to start with (auto-converted from C#):

Dim ofd = New OpenFileDialog()
ofd.Title = "Select Data File"

If ofd.ShowDialog() = DialogResult.OK Then
    Dim data As New StreamReader(ofd.FileName.ToString())
    While data.Read() <> " "c


    End While
    ' Read past Calculated
    While data.Read() <> " "c


    End While
    ' Read past Concentrations
    Dim concentBuilder As New StringBuilder()
    Dim last As Integer

    Dim concentrations As New List(Of Double)()
    Do
        last = data.Read()
        If last = " "c OrElse last = -1 Then
            Dim concentStr As String = concentBuilder.ToString()
            concentBuilder.Remove(0, concentBuilder.Length)

            Dim lastConcentration As Double
            Dim parseSuccess As Boolean = [Double].TryParse(concentStr, lastConcentration)
            If Not parseSuccess Then
                Console.[Error].WriteLine("Failed to parse: {0}", concentStr)
            Else
                concentrations.Add(lastConcentration)
            End If
        Else
            concentBuilder.Append(CChar(last))
        End If
    Loop While last <> -1

    For Each d As Double In concentrations
        Console.WriteLine(d)
    Next
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.