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

Visual Basic Question here. So there\'s a screenshot of my entire assignment bel

ID: 3604697 • Letter: V

Question

Visual Basic Question here. So there's a screenshot of my entire assignment below, and I have it mostly coded. I'm having trouble figuring out how to poll the .txt file and get a count by specific line. So for example, this codes it to save either R, D, or I but I cannot figure out how to get it to display a count of each specific party in my txt boxes in the app. Here's the assignment...

Here's the interface I've made for this (based on the assignment directions)

For reference, my output boxes are txtDem, txtRep and txtInd

Here's my code so far. It's really a matter of getting the btnDisplay code correct which I'm having the trouble with.

Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
Dim outFile As IO.StreamWriter
outFile = IO.File.AppendText("party.txt")
outFile.WriteLine(txtParty.Text & txtAge.Text)
outFile.Close()
txtParty.Text = String.Empty
txtAge.Text = String.Empty
txtParty.Focus()
End Sub

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("party.txt") = True Then
inFile = IO.File.OpenText("party.txt")
Else
MessageBox.Show("Can't find the file", "party.txt",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If

End Sub

Any help with this is appreciated.

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 ClearlyVB20121Chap22 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

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