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

VISUAL BASIC: Write a program that reads in the text file \"names.txt\" provided

ID: 3735336 • Letter: V

Question

VISUAL BASIC: Write a program that reads in the text file "names.txt" provided in the assignment folder in blackboard. You MUST use the OpenTextFileReader approach and read each line individually in a loop checking for the end-of-file condition. You are allowed to use a fixed size array for this project. Assume a maximum file size of 100 entries. Make sure you count how many you retrieve from the file! Your program should then display all of the names, in the provided order, in a list box on the left hand side of the form. You should then create two buttons on your form. One button will sort the list into ascending order, the other descending order. You may use any sort algorithm that you want though I would suggest the Bubble Sortis easiest to code. You must write your own version of the sort algorithm. The result of the sort should then be displayed in a separate list box on the right hand side of the form. Something similar to the following: NOTE: You must write your own SORT routine. You are not allowed to use the internal Visual Basic Sort.

In Visual Basic, if you simply specify the filename in the
OpenTextFileReader() function call, then VB will expect the file to reside
in the binDebug subdirectory where you are storing your source code. In my
case the file is actually stored in: D: Assignment_4inDebug
For the sake of this assignment, place the data file at the top level of your
project directory. As an example if I am developing my application in the
folder D:Assignment_4, I would place the names.txt file in that directory. I
would then change the function call to be:
My.Computer.FileSystem.OpenTextFileReader ("D: Assignment_4 ames.txt")
NOTE: Ensure that you submit a well written and well documented
program. Also make sure that your variable and control names are
representative of their use/function.

txt file content:

Edwin Hubble
Robert Oppenheimer
Jonas Salk
Richard Feynman
James Watson
Carl Sagan
Annie Jump Cannon
Henrietta Leavitt
Dian Fossey
Barbara McClintock
Grace Hopper
Maria Mitchell
Al-Zahrawi
Al-Zarqali
Ibn al-Shatir
Al-Khalili
al-Jazari
Al-Idrisi
Ruby Payne-Scott
Gregor Mendel
Erwin Schrodinger
Lise Meitner
Isaac Newton
Michael Faraday
Charles Babbage
Charles Darwin
William Thomson Kelvin
Alan Turing
Stephen Hawking
Jane Goodall
Frederick Banting
Sylvia Edlund
Roberta Bondar
Kathleen I. Pritchard
Margaret Chan
Zhang Heng
Zu Chongzhi
Qin Jiushao
Nikola Tesla
Niels Bohr
Robert Boyle
Edmond Halley
Blaise Pascal
Louis Pasteur
Irene Joliot-Curie
Johannes Kepler
Albert Einstein
Maria Goeppert-Mayer
Caroline Herschel
Carl Friedrich Gauss
Hippocrates
Aristotle
Archimedes
Edward Teller
Srinivasa Ramanujan
C. V. Raman
Satyendra Nath Bose
Subrahmanyan Chandrasekhar
Brahmagupta
Ada Yonath
Leonardo da Vinci
Nicolaus Copernicus
Galileo Galilei
Ernest Rutherford
Marie Curie
Maria Sklodowska-Curie
Dmitri Mendeleev
Vera Faddeeva
James Watt
Alexander Fleming
Anders Celsius
Alfred Nobel

How file should look:

Explanation / Answer

Imports System.IO

Public Class Form1
Dim linestxt(100) As String
Dim NumberOfLines As Integer = 0
Dim l As String


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sr As New StreamReader("C:Assignment ames.txt")

Do While sr.Peek >= 0
l = sr.ReadLine()
NamesList.Items.Add(l)
linestxt(NumberOfLines) = l
NumberOfLines += 1
Loop
TextBox1.Text = NumberOfLines.ToString()
  

sr.Close()
sr.Dispose()
End Sub

REM FOR SORTING IN ASCENDING ORDER
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim exch As Integer
Dim t As String
exch = 1
While (exch > 0)
exch = 0
For i = 0 To NumberOfLines - 2
If (System.String.Compare(linestxt(i), linestxt(i + 1)) >= 0) Then
t = linestxt(i)
linestxt(i) = linestxt(i + 1)
linestxt(i + 1) = t
exch = exch + 1
End If
Next
End While
For i = 0 To NumberOfLines - 1
sortedList.Items.Add(linestxt(i))
Next
TextBox2.Text = sortedList.Items.Count.ToString()

End Sub
REM FOR SORTING IN DESCENDING ORDER
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim exch As Integer
Dim t As String
sortedList.Items.Clear()


exch = 1
While (exch > 0)
exch = 0
For i = 0 To NumberOfLines - 2
If (System.String.Compare(linestxt(i), linestxt(i + 1)) <= 0) Then
t = linestxt(i)
linestxt(i) = linestxt(i + 1)
linestxt(i + 1) = t
exch = exch + 1
End If
Next
End While
For i = 0 To NumberOfLines - 1
sortedList.Items.Add(linestxt(i))
Next
TextBox2.Text = sortedList.Items.Count.ToString() ' TO SHOW COUNT
End Sub