2010-03-28 54 views
2

我收到錯誤「格式異常是未處理的‘做雖然objectReader.Peek <> -1 ’之後。任何幫助將是美妙的。錯誤在VB.Net代碼

'Date: Class 03/20/2010 
'Program Purpose: When code is executed data will be pulled from a text file 
'that contains the named storms to find the average number of storms during the time 
'period chosen by the user and to find the most active year. between the range of 
'years 1990 and 2008 

Option Strict On 

Public Class frmHurricane 

    Private _intNumberOfHuricanes As Integer = 5 
    Public Shared _intSizeOfArray As Integer = 7 
    Public Shared _strHuricaneList(_intSizeOfArray) As String 
    Private _strID(_intSizeOfArray) As String 
    Private _decYears(_intSizeOfArray) As Decimal 
    Private _decFinal(_intSizeOfArray) As Decimal 
    Private _intNumber(_intSizeOfArray) As Integer 

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles MyBase.Load 
     'The frmHurricane load event reads the Hurricane text file and 
     'fill the combotBox object with the data. 

     'Initialize an instance of the StreamReader Object and declare variable page 675 on the book 
     Dim objectReader As IO.StreamReader 
     Dim strLocationAndNameOfFile As String = "C:\huricanes.txt" 
     Dim intCount As Integer = 0 
     Dim intFill As Integer 
     Dim strFileError As String = "The file is not available. Please restart application when available" 

     'This is where we code the file if it exist. 
     If IO.File.Exists(strLocationAndNameOfFile) Then 
      objectReader = IO.File.OpenText(strLocationAndNameOfFile) 
      'Read the file line by line until the file is complete 
      Do While objectReader.Peek <> -1 
       **_strHuricaneList(intCount) = objectReader.ReadLine() 
       _strID(intCount) = objectReader.ReadLine() 
       _decYears(intCount) = Convert.ToDecimal(objectReader.ReadLine()) 
       _intNumber(intCount) = Convert.ToInt32(objectReader.ReadLine()) 
       intCount += 1** 
      Loop 
      objectReader.Close() 

     'With any luck the data will go to the Data Box 
     For intFill = 0 To (_strID.Length - 1) 
      Me.cboByYear.Items.Add(_strID(intFill)) 
     Next 
    Else 
     MsgBox(strFileError, , "Error") 
     Me.Close() 

    End If 
End Sub 

回答

0

而不是使用Convert.ToDecimalConvert.ToInt32,請嘗試使用Decimal.TryParse()Int32.TryParse()。如果他們不解析,你知道你的文件安裝不正確。如果他們解析,那麼你已經把值加載到一個變量供你使用。

編輯:

這樣使用它。

Dim myDecimal As Decimal 

If Decimal.TryParse(objectReader.ReadLine(), myDecimal) Then 
    'your string successfully parsed. Use myDecimal however you want. It has the parsed value. 
Else 
    'your string is not a decimal. Now that you know that, you can handle this situation here without having to catch an exception. 
End If 
+0

錯誤重載決策失敗,因爲沒有可訪問「的TryParse」接受此數目的參數。 \t 試過了,這是我得到的 – Michael 2010-03-28 20:15:59

1

放了突破點在這些線路上,並通過您的代碼步:

_decYears(intCount) = Convert.ToDecimal(objectReader.ReadLine()) 
_intNumber(intCount) = Convert.ToInt32(objectReader.ReadLine()) 

無論是由ReadLine返回不在的時候傳遞到轉換器正確的十進制或整數格式,您需要添加一些嘗試/圍繞do-while循環操作捕獲塊以適當的方式處理它,並確保數據的格式符合您的預期,因爲在這種情況下使用了錯誤的部分。

此外,每個撥打ReadLine的電話將返回一個全新的行,Peek檢查將不會考慮這些行。只要你可以相信你的數據沒問題。