vb.net - 为什么在尝试对csv文件进行冒泡排序时会出现此错误?

时间:2013-09-03 08:27:26

标签: vb.net csv

我有一个csv文件,我正在尝试按数据排序(数字形式)

csv文件: 日期,姓名,电话号码,教师姓名

1308290930,吉姆,041231232,寿司

123123423,杰里米,12312312,艾伯特

我得到的错误是:从字符串“jeremy”到“double”类型的转换无效 即使在我的代码中没有我提到双重...

我的代码:

Public Class Form2
    Dim currentRow As String()
    Dim count As Integer
    Dim one As Integer
    Dim two As Integer
    Dim three As Integer
    Dim four As Integer
    'concatenation / and operator
    'casting

    Dim catchit(100) As String
    Dim count2 As Integer
    Dim arrayone(4) As Decimal
    Dim arraytwo(4) As String
    Dim arraythree(4) As Decimal
    Dim arrayfour(4) As String


    Dim array(4) As String

    Dim bigstring As String
    Dim builder As Integer
    Dim twodata As Integer


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load




        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("D:\completerecord.txt")
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")
            Dim currentRow As String()
            Dim count As Integer
            Dim currentField As String
            count = 0
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()

                    For Each currentField In currentRow
                        ' makes one array to contain a record for each peice of text in the file

                        'MsgBox(currentField) '- test of Field Data
                        ' builds a big string with new line-breaks for each line in the file

                        bigstring = bigstring & currentField + Environment.NewLine


                        'build two arrays for the two columns of data
                        If (count Mod 2 = 1) Then

                            arraytwo(two) = currentField
                            two = two + 1

                            'MsgBox(currentField)
                        ElseIf (count Mod 2 = 0) Then
                            arrayone(one) = currentField
                            one = one + 1
                        ElseIf (count Mod 2 = 2) Then
                            arraythree(three) = currentField
                            three = three + 1
                        ElseIf (count Mod 2 = 3) Then
                            arrayfour(four) = currentField
                            four = four + 1



                        End If

                        count = count + 1
                        'MsgBox(count)
                    Next

                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Error Occured, Please contact Admin.")
                End Try
            End While
        End Using
        RichTextBox1.Text = bigstring
        ' MsgBox("test")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NoMoreSwaps As Boolean
        Dim counter As Integer

        Dim Temp As Integer
        Dim Temp2 As String
        Dim listcount As Integer
        Dim builder As Integer
        Dim bigString2 As String = ""

        listcount = UBound(arraytwo)
        'MsgBox(listcount)
        builder = 0
        'bigString2 = ""
        counter = 0
        Try

            'this should sort the arrays using a Bubble Sort
            Do Until NoMoreSwaps = True
                NoMoreSwaps = True
                For counter = 0 To (listcount - 1)


                    If arraytwo(counter) > arraytwo(counter + 1) Then
                        NoMoreSwaps = False

                        If arraytwo(counter + 1) > 0 Then

                            Temp = arraytwo(counter)
                            Temp2 = arrayone(counter)

                            arraytwo(counter) = arraytwo(counter + 1)
                            arrayone(counter) = arrayone(counter + 1)

                            arraytwo(counter + 1) = Temp
                            arrayone(counter + 1) = Temp2

                        End If
                    End If

                Next
                If listcount > -1 Then
                    listcount = listcount - 1
                End If

            Loop

            'now we need to output arrays to the richtextbox first we will build a new string
            'and we can save it to a new sorted file
            Dim FILE_NAME As String = "D:\sorted.txt"
            'Location of file^ that the new data will be saved to

            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
                'If D:\sorted.txt exists then enable it to be written to

                While builder < listcount
                    bigString2 = bigString2 & arraytwo(builder) & "," & arrayone(builder) + Environment.NewLine

                    objWriter.Write(arraytwo(builder) & "," & arrayone(builder) + Environment.NewLine)

                    builder = builder + 1
                End While
                RichTextBox2.Text = bigString2

                objWriter.Close()
                MsgBox("Text written to log file")
            Else
                MsgBox("File Does Not Exist")
            End If

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我认为这一行是问题

arrayone(one) = currentField

这里尝试将字符串转换为double。你必须使用这样的东西:

arrayone(one) = Double.Parse(currentField)

或者让它更安全:

Dim dbl As Double
If Double.TryParse(currentField, dbl) Then
  arrayone(one) = dbl
Else
  arrayone(one) = 0.0
End If