Download presentation
Presentation is loading. Please wait.
Published byJames Chapman Modified over 9 years ago
1
CS 4 Intro to Programming in Visual Basic Files and Loops (2) Patchrawat Uthaisombut University of Pittsburgh 1
2
Example: Employee Skills We have a data file that contain employee names and a list of skills for each employee. We want to list all the employee with VB skills.
3
Data File James K. Word VB ---end--- Lisa B. Excel Access ---end--- Jean L. Word Excel VB PowerPoint ---end--- James K. Word VB ---end--- Lisa B. Excel Access ---end--- Jean L. Word Excel VB PowerPoint ---end---
4
Data File Format The file consists of the information about 1 or more employees. The information of each employee is on 2 or more lines. The first line for each employee is their name. Then each skill is listed on a separate line After the last skill, there is a line with the string “---end---”
5
Interface Design
6
High-Level Idea Open file Process 1 employee at a time – Process 1 skill at a time If the employee has VB skill, display his/her name Close file
7
Idea File Nested Loop – Loop on the employees – Loop on the skills of each employee If block – Check if the employee has VB skills
8
File Dim sr As IO.StreamReader sr = IO.File.OpenText("EmployeeSkills.txt") ‘ Process employees ‘ use sr.ReadLine to read 1 line from file ‘ use sr.Peek to check if there is more data in the file sr.Close()
9
Inner Loop: process 1 employee Read name Read zero or more skills using a Loop If one of the skills is “VB”, display employee name in the list box The loop terminates when we read “---end---” Jean L. Word Excel VB PowerPoint ---end---
10
Detecting VB skills … skill = sr.ReadLine If skill = "VB" Then lstDisplay.Items.Add( _ name & " has VB skills.") End If …
11
Inner Loop: process 1 employee Read name Read zero or more skills using a Loop If one of the skills is “VB”, display employee name in the list box The loop terminates when we read “---end---” How do we distinguish between a skill and “---end---” ? Do we need to distinguish name and skill? Jean L. Word Excel VB PowerPoint ---end---
12
Distinguishing skill and ---end--- Use IF block If skill = "---end---" Then ‘ now we know there is no more skills End If
13
Terminating The Loop Set a flag set in the IF block in the previous slide Use this flag as the loop condition endOfRecord = False Do skill = sr.ReadLine ‘... If skill = "---end---" Then endOfRecord = True End If Loop Until endOfRecord
14
Outer Loop: process whole file (multiple employees) Use Peek to check “End Of File” Do While sr.Peek <> -1 ‘... Loop
15
Dim sr As IO.StreamReader = sr = IO.File.OpenText(“EmployeeSkills.txt”) Dim name As String Dim skill As String Dim endOfRecord As String Do While sr.Peek <> -1 name = sr.ReadLine endOfRecord = False Do skill = sr.ReadLine If skill = "VB" Then lstDisplay.Items.Add(name & " has VB skills.") End If If skill = “---end---” Then endOfRecord = True End If Loop Until endOfRecord Loop sr.Close()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.