Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My app functions, but when I enter 1 democrats, the totals show 3 Dem, 1 Rep, an

ID: 3702337 • Letter: M

Question

My app functions, but when I enter 1 democrats, the totals show 3 Dem, 1 Rep, and 1 Indep.

What's wrong with my array? here is the code:

Public Class FrmMain

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Me.Close()

    End Sub

    Private Sub FrmMain_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        lstparty.Items.Add("D")

        lstparty.Items.Add("R")

        lstparty.Items.Add("I")

        lstparty.SelectedIndex = 0

    End Sub

    Private Sub txtAge_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtAge.KeyPress

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso

                e.KeyChar <> ControlChars.Back Then

            e.Handled = True

        End If

    End Sub

    Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click

        Dim outFile As IO.StreamWriter

        If txtAge.Text <> String.Empty Then

            outFile = IO.File.AppendText("pao.txt")

            outFile.WriteLine(lstParty.SelectedItem.ToString)

            outFile.WriteLine(txtAge.Text)

            outFile.Close()

        End If

    End Sub

    Private Sub btnTotals_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTotals.Click

        Dim strParty As String

        Dim strAge As String

        Dim inFile As IO.StreamReader

        Dim intTotalDem As Integer

        Dim intTotalRep As Integer

        Dim intTotalInd As Integer

        If IO.File.Exists("pao.txt") = True Then

            inFile = IO.File.OpenText("pao.txt")

            Do Until inFile.Peek() = -1

                strParty = inFile.ReadLine

                strAge = inFile.ReadLine

                If strParty = "D" Then

                    intTotalDem += 1

                ElseIf strParty = "R" Then

                    intTotalRep += 1

                Else

                    intTotalInd += 1

                End If

            Loop

            inFile.Close()

            txtDemo.Text = intTotalDem.ToString

            txtRep.Text = intTotalRep.ToString

            txtInde.Text = intTotalInd.ToString

        Else

            MessageBox.Show("pao.txt file not found.", "Politcal Awareness Organization", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

    End Sub

End Class

8. During July and August of each year, the Political Awareness Organization (PAO) sends a questionnaire to the voters in its district. The questionnaire asks each voter for his or her political party (Democratic, Republican, or Independent) and age. From the returned questionnaires, the organization's secretary tabulates the number of Democrats, Republicans, and Independents in the district. The secretary wants an application that she can use to save each respondent's information (political party and age) to a sequential access file. The application should also calculate and display the number of voters in each political party. Create a Visual Basic Windows application. Use the following names for the solution and project, respectively: PAO Solution and PAO Project. Save the application in the ClearlyVB2012|Chap22 folder. Change the name of the form file on your disk to frmMain.vb. If necessary change the form's name to frmMain. Create the interface shown in Figure 22-15. The Party text box should accept only the Backspace key and the letters D, d, R, r I, or i. The Age text box should accept only numbers and the Backspace key. Code the Click event procedures for the Write to File and Display Totals buttons. Save the solution and then start and test the application. Close the Code Editor window and then close the solution.

Explanation / Answer

ANSWER:

To display the count there are many appraches below the simple one for this requirement.

# using the while loop read all the content of the file and store it in varaible

# now you have all the content of the file in the string variable

# now convert all the string in lowecase since use can enter D,d,R,r,I,i

# by converting to lowercase we don't have to care in which case user enter the value

# now using for loop extract each character from the string and check if they are the required one

# if the character is r,d,i then increment their respective variable

# now display variable data in the textfeild

BELOW IS THE CODE for btnDisplay_Click function

read comments for more detail

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' reads names from a sequential access file

' and displays them in the interface

' declare variables

Dim inFile As IO.StreamReader

' clear previous names from the Contestants box

txtParty.Text = String.Empty

' determine whether the file exists

If IO.File.Exists("f:\party.txt") = True Then

inFile = IO.File.OpenText("f:\party.txt")

'######### ADDED THE BELOW CODE

'Create String to store the file content

Dim fileData As String

'Read all the lines in file and append it to the fileData

'Read while there is content in file

While (inFile.Peek >= 0)

fileData += inFile.ReadLine()

End While

'convert all the text to lowercase

'By this we save time by not comparing the uppper case

'eg : if data stored is D98 it will get converted to d98

'Since there is no difference between D and d we can convert to lowercase [as per application requirement]

' ToLower() convert the string in lowecase

fileData = fileData.ToLower()

'Variables to store the count of D,R,I

'Initalise them with 0 to remove any garbage value

Dim dcount = 0, rcount = 0, icount = 0

'Run the loop to get the each char in the "fileData" and compare if it is D,I,R

'Loop need to be run till the length of the "fileData" (means the number of char)

'we just check each character if it is i,r,d then increment their respective variable

For x = 1 To fileData.Length Step 1

'get the char at position

' if filedata contain "D54 r65" then value at 1 position is D

' GetChar return the character at specific position

Dim charx = GetChar(fileData, x)

Console.WriteLine(charx)

'check if i then increment

If charx = "i" Then

icount = icount + 1

End If

'check if d then increment

If charx = "d" Then

dcount = dcount + 1

End If

'check if r then increment

If charx = "r" Then

rcount = rcount + 1

End If

Next

'^^ end the loop

'now set the value in the textFeild

txtd.Text = dcount 'set the value of democratic textfeild

txti.Text = icount 'set the value of Republician textFeid

txtr.Text = rcount 'set the value of independent textFeild

'Now close the FileStream

inFile.Close()

'###############################3

Else

MessageBox.Show("Can't find the file", "party.txt",

MessageBoxButtons.OK,

MessageBoxIcon.Information)

End If

End Sub

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote