tags:

views:

179

answers:

3

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!

+2  A: 
  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
Matthew Flaschen
+2  A: 

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.

Jose Basilio
+3  A: 

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())
Lucas Jones
@person-b hey your code is not working, it's not parsing
I__
Oh. :-) Can you give me an exact error message or exception?
Lucas Jones
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
I__
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.
Lucas Jones
i did all your changes and still msgbox is returning 0
I__
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
I__
Can you paste your current code onto http://pastebin.com, then post the URL here. It makes the code easier to read. Sorry about all this.
Lucas Jones
Oh. Sorry. I posted the comment before your previous one.
Lucas Jones
hey this thing wont let me paste more than 6 characters unfortunately, but i have it exactly as you suggested to me, thanks so much for help btw
I__
it looks like what this is doing is for each field in fields its putting a line of the file and it removes all spaces. fields = calculationText.Split(" ")
I__
and this is in correct, i actually need it so read individual numbers which are separated by spaces, not whole lines
I__
As I understand it, the code you have is: http://pastebin.com/m750aa096 . This seems to be slightly off, and I'll fix it if you can verify it is in fact the code you have. Also, my internet is going quite slow, so please be patient.
Lucas Jones
yes............
I__
@alex. Ok. There's a minor problem here - I'll post the new code ASAP.
Lucas Jones
http://pastebin.com/m230f55c1 - tell me if this works.
Lucas Jones
(http://pastebin.com/m117a8cbb) with highlighting.
Lucas Jones
it does not work, as i mentioned the problem is not with this code, it is with calculationtext.split
I__
Is the problem that some of the entries contain newlines? I don't quite understand what you said in your comment.
Lucas Jones
ReadAllText may become an issue if the list becomes very large, since it requires the entire file be read at once into a huge string.
Matthew Flaschen
person-b each of field in fields is a whole line of the file, it is not an individual digit
I__
I don't have VB next to me to check it, unfortunately. The expected behaviour is that every field should contain a number - ie "9.82", or "?????" or another word. The program checks to see whether the field is a number. If it is, it adds it to the list. In the input file, are the fields separated by spaces or by newlines? This may be the problem.
Lucas Jones