Miranda Lynn
Miranda Lynn

Reputation: 67

Getting wrong output in label

I have code that I have written, that has 3 labels for number of hurricanes, average hurricanes, and the year with the most hurricanes from a txt file. The code is working and the first 2 labels are displaying the correct results. However the last label is displaying the number of the year with the most hurricanes instead of the year.

Here is what I have:

   Option Strict On

Public Class frmHurricaneStatistics

'   Class level Private variables.
Public Shared _intSizeOfArray As Integer = 20
Private _strYears(_intSizeOfArray) As String
Private _intNumberOfHurricans(_intSizeOfArray) As Integer

Private Sub frmHurricaneStatistics_Load(sender As Object, e As EventArgs
                                        ) Handles MyBase.Load

    '   This load event reads the inventory text file and fills
    '   the ComboBox object with the Hurricane Statistics.

    '   Initialize an instace of the streamreader object and declare variables.
    Dim objReader As IO.StreamReader
    Dim strHurricaneStatistics As String = "Hurricanes.txt"
    Dim intCount As Integer = 0
    Dim intFill As Integer
    Dim strFileError As String = "The file is not available. Please restart the
        application when the file is available."

    '   Verify the Hurricane.txt file exists.
    If IO.File.Exists(strHurricaneStatistics) Then
        objReader = IO.File.OpenText(strHurricaneStatistics)

        '   Read the file line by line until the file is completed.
        Do While objReader.Peek <> -1
            _strYears(intCount) = objReader.ReadLine()
            _intNumberOfHurricans(intCount) = Convert.ToInt32(objReader.ReadLine())
            intCount += 1
        Loop
        objReader.Close()

        '   The ComboBox objext is filled with the Years for Hurricanes.
        For intFill = 0 To (_strYears.Length - 1)
            cmbYears.Items.Add(_strYears(intFill))
        Next
    Else
        MsgBox(strFileError, , "Error")
        Close()

        '   If ComboBox is filled then enable the Display Statistics button.
        '   btnDisplayStatistics.Enabled = True
    End If
End Sub

Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs
                                       ) Handles btnDisplayStatistics.Click

    '   This click event calls the sub procedures for the selected years and
    '   the number of hurricans in that year.
    Dim intSelectedYear As Integer
    Dim strMissingSelection As String = "Missing Selection"
    Dim strSelectAYearError As String = "Please Select a Year"

    '   If the ComboBox object has a selection, Display Statistics.
    If cmbYears.SelectedIndex >= 0 Then
        intSelectedYear = cmbYears.SelectedIndex
    Else
        MsgBox(strSelectAYearError, , strMissingSelection)
    End If

    '   The procedure MakeLabelsVisible Is called to display the labels
    '   And the results.
    MakeLabelsVisible()

    Dim intAverage As Double
    Dim intYear As Integer


    For intIndex As Integer = 0 To _intNumberOfHurricans.Length - 1
        If intYear < _intNumberOfHurricans(intIndex) Then
            intYear = _intNumberOfHurricans(intIndex)
        End If
        intAverage = intAverage + _intNumberOfHurricans(intIndex)
    Next

    intAverage = intAverage / _intNumberOfHurricans.Length


    '   Display the statistics for the Storm Average in the selected Year
    '   and the most active year within the range of year.

    lblNumberOfHurricanes.Text = "The Number of Hurricanes in the Year " &
        _strYears(intSelectedYear) & " is " & _intNumberOfHurricans(intSelectedYear).ToString() & "."
    lblAvergeNumberHurricanes.Text = "The Average Number of Storms was " & FormatNumber(intAverage, 0) & " Hurricanes."
    lblMostStorms.Text = "The Year " & intYear & " Had The Most Storms Between " & (
        _strYears(20) & " And " & (_strYears(0).ToString))




End Sub




Private Sub MakeLabelsVisible()

    '   This procedure displays the labels with the calculated results
    lblNumberOfHurricanes.Visible = True
    lblAvergeNumberHurricanes.Visible = True
    lblMostStorms.Visible = True




End Sub

Updated code.

Upvotes: 1

Views: 88

Answers (2)

p3tch
p3tch

Reputation: 1495

Looks like you're just populating intYear with the number of hurricanes?

intYear = _intNumberOfHurricans(intIndex)

I can't see where you're wanting to get a year value from. Does one even exist? Please post the rest of the code

Edit:

From what I understand (correct me if I'm wrong), you want the year that had the highest number of hurricanes? If so

Try

For intIndex As Integer = 0 To _intNumberOfHurricans.Length - 1
    If _intNumberOfHurricans(intIndex) = _intNumberOfHurricans.Max Then
        intYear = Integer.Parse(_strYears(intIndex))
    End If
    intAverage = intAverage + _intNumberOfHurricans(intIndex)
Next

What I'm doing here is looking for the highest value in _intNumberOfHurricans and comparing it to the number of hurricanes in the current iteration. If they're the same, then we are at the year with the highest number of hurricanes, so we populate intYear with _strYears(but as an Integer).

This code isn't perfect. For example, if the highest amount of hurricanes is 100, but there are 2 years where there are 100 hurricanes, it will only give the latest year, not the first year there were 100 hurricanes.

Upvotes: 2

Berkay Yaylacı
Berkay Yaylacı

Reputation: 4513

Because you set;

intYear = _intNumberOfHurricans(intIndex)

Not the year, number of hurricans. That should have point to a Year property.

intYear = _intNumberOfHurricans(intIndex).Year

Hope helps.

Upvotes: 1

Related Questions