Question: THIS IS IN VISUAL BASIC I need to find the month that has the maximum savings from a list in a txt file called savings.txt

THIS IS IN VISUAL BASIC

I need to find the month that has the maximum savings from a list in a txt file called savings.txt and write a text on a label "The month of ?(name of the month) has the biggest savings. So far I have finished how to show the savings for selected month and to show the average for all the months.

What I need is how to find the biggest number from the list and instead of the value for that month I use the name of the month. First I need to find the maximum value of the list and then relate that index to the name of the month.

This is what I have done so far --------------------------------------------------------------------------------------------------------

Public Class FormSHMES

Public Shared _intSizeOfArray As Integer = 12 Private _strMonthId(_intSizeOfArray) As String Private _decMonthlySavings(_intSizeOfArray) As Decimal Private _decMAver As Decimal Private Sub FormSHMES_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim objReader As IO.StreamReader Dim strLocationAndNameOfFile As String = "savings.txt" Dim intCount As Integer = 0

Dim strFileError As String = "The file is not available. Restart when file is available."

If IO.File.Exists(strLocationAndNameOfFile) Then objReader = IO.File.OpenText(strLocationAndNameOfFile)

Do While objReader.Peek <> -1 Dim strMonth As String = objReader.ReadLine Dim strValue As String = objReader.ReadLine

If strMonth <> "" Then cboxMonths.Items.Add(strMonth) _decMonthlySavings(intCount) = Convert.ToDecimal(strValue) _strMonthId(intCount) = Convert.ToString(strMonth)

Dim decMonthlyTotal As Decimal = 0 For i As Integer = 0 To 11 decMonthlyTotal = decMonthlyTotal + _decMonthlySavings(i) _decMAver = decMonthlyTotal / 12

Next

End If

intCount += 1 Loop

objReader.Close()

Else MsgBox(strFileError, , "Error") Close() End If End Sub

Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs) Handles btnDisplayStatistics.Click If cboxMonths.SelectedIndex >= 0 Then lblMonthlySavings.Text = "The electric savings for " & _strMonthId(cboxMonths.SelectedIndex).ToString() & " is $" & _decMonthlySavings(cboxMonths.SelectedIndex).ToString() End If lblAverageMonthlySavings.Text = "The average monthly: $" & _decMAver.ToString() 'lblBestMonth.Text = ??? & " is the month with most savings"

End Sub End Class

------------------------------------------------------------------------------------------------------------savings.txt file has the savings for all the months in a year for each month: January 38.17 February 41.55 March 27.02 April 25.91 May 3.28 June 18.08 July 45.66 August 16.17 September 3.98 October 17.11 November 25.78 December 11.03

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!