2016-12-12 81 views
0

我一直在閱讀各種論壇,並且一直在努力尋找真正幫助我解決問題底部的任何文章,所以我正在寫在萬不得已的從文本文件中獲取文本/整數並將數據插入到vb中的二維數組中

我有一個包含該數據的文本文件:

瓊斯14個

艾布拉姆斯15

史密斯19

瓊斯9

亞歷山大22

史密斯20

史密斯17

泰勒42

瓊斯2

胡珀12

瓊斯11

我需要重複的名稱的字母順序,然後命令名,由

我第一次開始通過嘗試做readalllines功能號(又名年齡)責令其按升序排列,然後把這些行到一個數組,代碼如下:

Dim orfi As String = "P:\names_ages.txt" 
arrline() = File.ReadAllLines(orfi) 

然後我試圖在空間的分隔符通過分割功能分割線:

Split(arrline(), " ") 

在嘗試此,我一直得到一個錯誤,告訴我我無法拆分數組中的字符串,這使我感到困惑,現在我不知道如何根據名稱和年齡將這些數據轉換爲二維數組。

如果任何人有任何想法,我是V感恩

全碼:

Imports System.IO 

模塊模塊1

Sub Main() 

    Dim info(name, age) As Integer 
    Dim orfi As String = "P:\names_ages.txt" 
    Dim c As Integer 
    Dim loopswap As Boolean 
    Dim temp As Integer 
    Dim maxIndex As Integer 
    Dim arrline() As String 
    maxIndex = arrline.Length - 1 

    'attempting to put lines into an array 
    Sub Main() 
    Dim lines() As String = File.ReadAllLines("P:\names_ages.txt") 
    Dim values(lines.Length - 1, 1) As String 
     For i As Integer = 0 To lines.Length - 1 
     Dim parts() As String = lines(i).Split(" ") 
      values(i, 0) = parts(0) 
      values(i, 1) = parts(1) 
    Next 

    'parsing file lines into an array and then further parsing them? 
    arrline() = File.ReadAllLines(orfi) 
    Split(arrline(), " ") 

    'bubble that sorts each line alphabetically 
    Do 
     loopswap = False 
     For c = 0 To maxIndex - 1 
      If arrline(c) > arrline(c + 1) Then 
       temp = arrline(c) 
       arrline(c) = arrline(c + 1) 
       arrline(c + 1) = temp 

       loopswap = True 
      End If 
     Next 
    Loop While loopswap 
    'bubble that sorts each line depending on age in ascending order 
End Sub 

前端模塊

+0

這聽起來像某種大學項目......也許提供完整的例程而不是單行。 –

+1

我應該注意''ReadAllLines()'返回一個字符串數組,並且您正在嘗試分割數組。我認爲你更好地執行while循環並使用'ReadLine()'。 –

+0

問題現在包含完整(寫得不好)的代碼。我真的很新,所以我很抱歉,如果這真的很糟糕閱讀哈哈 – yttamyttam

回答

0

這是廣義上你想要,這不是完美的,但它會讓你更好地理解需要什麼。我添加了一個結構來保持組織。

如果您想了解sr.peek(),請參閱:StreamReader.Peek()

希望這有助於。

Imports System.IO 
Module Module1 
    Sub Main() 
     dim fileContents as List(Of CustomerInfo) = ReadFile("c:\names_ages.txt") 
     for each customer As CustomerInfo In fileContents 
      console.WriteLine("Name: " + customer.Surname & ", Age: " & customer.age.ToString()) 
     Next 
     Console.ReadLine() 
    End Sub 
    Structure CustomerInfo 
     Dim Surname As String 
     Dim Age As Integer 
    End Structure 
    Private Function ReadFile(path As String) As List(Of CustomerInfo) 
     Dim custInfo As New List(Of CustomerInfo) 
     Dim sr As StreamReader = New StreamReader(path) 
     Do While sr.Peek() >= 0 'Check that the new line contains a character 
      Dim ci As New CustomerInfo 
      Dim customer As New List(Of String) 'Declare a new list 
      customer = sr.ReadLine().Split(" ").ToList() 'Split string and call ToList() 
      ci.Surname = customer(0).Trim() 'Select First element in list 
      ci.Age = customer(1).ToString().Trim() 'Select second element in list 
      'This is not a robust way to do things... It relies on data fitting a very specific format. 
      custInfo.Add(ci) 'Add structure to the list of customerInfo 
     Loop 
     Return custInfo 
    End Function 
End Module 

爲排列......我不知道,如果你允許使用Linq但在這裏是一種(不冒泡排序,授...)

Dim sortedList = From f In fileContents Order By f.Surname 
+0

非常感謝丹尼爾,這對於未來的編碼非常有用並且非常有用。唯一的問題是我的教授想讓我用泡泡或插入排序來排列名字和年齡,因此我推測我需要一個二維數組? – yttamyttam

+0

除非特意要求2D數組,否則結構列表就足夠了,甚至可能會得到標記。當循環訪問列表時,可以執行list.surname和list.age,而不是訪問數組中的位置。它更具可讀性並且提供了一個簡潔的結構。注:該列表類似於二維數組。 –

+0

我們在大學時使用了vis studio 2010,它似乎沒有拿起流命令?感謝您澄清列表珍聞! – yttamyttam

相關問題