Download - Strings and I/O
04/21/23 1
Strings and I/OStrings and I/O
ISAT 252:Analytical Methods ISAT 252:Analytical Methods IVIV
VB.NETVB.NET
AssignmentAssignment
Should have read Chapter 3 pp. 231-241
String Properties and String Properties and MethodsMethods
See pp. 231-241 What does each of these do? str.Length
str.Substring(m, n)
str.IndexOf(str2)
str.ToUpper
str.ToLower
str.Trim
Remember, string indexes start at 0
Conversion of Data typesConversion of Data types
To convert values in a data type to the equivalent value in another data type
Converts non-numeric data (strings|text values) to numeric data Double.Parse() or Integer.Parse() method - Use
to convert input text in a text box/string variable into a number
TotalItemsInteger = Integer.parse("123")
SalesPriceDouble = Double.Parse(txtPrice.Text)
TotalScoreDouble = TotalScoreDouble + _ Double.Parse(txtScore.Text)
ConversionConversion Converts a non-string value in in its equivalent string
of characters
ToString Method Example*
lblOutput.text = (1 + 2 – 3).ToString
lblOutput.text = intItems.ToString
lblOutput.text = (dblTotal*(1 + dblTax)).ToString
*note I am using the shorter naming convention here for a label control. Our book uses OutputLabel.Text = (1 + 2 – 3).ToString, etc.
Problem – compute area of Problem – compute area of circlecircle
Allow user to enter radius
Pseudocode: Declare variables Get input and convert Compute area Display output
Example: compute the area of a Example: compute the area of a circlecircle
Private Sub btnCompute_Click(..) handles _btnCompute.Click
' compute the area of a circleConst dblPI as Double = 3.14
Dim dblRadius As DoubleDim dblCircleArea As Double
‘Convert the input radius into a number dblRadius = CDbl(txtRadius.Text)
'compute the area of the circle dblCircleArea = dblPI * dblRadius ^ 2 'output the area of the circle txtOut.Text = "The area of the circle is " + _ dblCircleArea.ToString & " inches“End Sub
Data declaration
Input and convert the appropriate values
Calculation
Output
Displaying Text OutputDisplaying Text Output
Labels Multiple lines, but it will not allow the user
to scroll when all the lines are not visible Use CStr or To.String to convert
numbers to string List Boxes
Use lstName.Items.Add( ) for each line Use CStr or To.String to convert
numbers to string
Message Boxes See p. 140
Displaying Text OutputDisplaying Text Output
TextboxTextbox When ReadOnly Property set to TRUE, a
textbox is strictly an output control You can display multiple lines in a textbox
by setting the property Multiline to TRUE You can allow the user to scroll
horizontally and vertically by setting the appropriate scrollbars in the property Scrollbars
Use CStr or To.String to convert numbers to string
Formatted OutputFormatted Output
Can adjust spacing by inserting blanks blanks should be within the quote marks works best with a fixed size font such as
Courier
Numeric Output can use pre-defined formats such as
“FormatCurrency” and “FormatPercent” can create custom columnar formats
using zones
Formatting Data for DisplayFormatting Data for Display
To display numeric data in a label or text box, first convert value to string.
Use ToString method
Format the data using formatting codes.Specifies use of dollar sign, percent sign,
and commasSpecifies number of digits that appear to
right of decimal point
DisplayTextBox.Text = NumberInteger.ToString()
Using Format Specifier Using Format Specifier CodesCodes
"C" codeCurrency — String formatted with dollar
sign, commas separating each group of 3 digits and 2 digits to the right of decimal point
"N" codeNumber — String formatted with commas
separating each group of 3 digits and 2 digits to the right of decimal point
Can specify number of decimal positionsExample: "C0" zero digits
Format Specifier CodesFormat Specifier Codes
Format Specifier Codes Name
C or c Currency
F or f Fixed-point
N or n Number
D or d Digits
P or p Percent
Format Specifier Code Format Specifier Code ExamplesExamples
Variable Value Code Output
totalDecimal 1125.6744 "C" $1,125.67
totalDecimal 1125.6744 "N0" 1,126
pinInteger 123 "D6" 000123
rateDecimal 0.075 "P" 7.50%
rateDecimal 0.075 "P3" 7.500%
rateDecimal 0.075 "P0" 8%
valueInteger -10 "C" ($10.00)
Getting InputGetting Input
From a TextboxFrom a Textbox Always a string Use Double.Parse or Integer.Parse if you
are putting values into number variableOR
You can use CDbl or CInt if you are putting values into number variables
From an Input Dialog Box See Textbook
Getting Input Getting Input From Files see p. 541-546
1. Declare the file
Dim readerVar As IO.StreamReader
2. Open the file
readerVar = IO.File.OpenText (filespec)
3. Read from file
strVar = readerVar.ReadLine
numVar = Double.Parse (readerVar.ReadLine)
( OR numVar = CDbl (readerVar.ReadLine) )
4. Close the file
readerVar.Close()
Example of Reading from Example of Reading from FilesFiles
Private Sub ReadFileButton_Click() Handles ReadFileButton.Click
Dim strName1 As String
Dim strName2As String
Dim fileNames As IO.StreamReader
fileNames = IO.File.OpenText("Names.txt")
‘Names.txt is in the Debug folder of the bin for the project
strName1 = fileNames.ReadLine
strName2 = fileNames.ReadLine
fileNames.Close()
txtOutput.Text = strName1+ " and " + strName2
End Sub
AssignmentsAssignments
Do Lab 6 and Lab 7 Answer the Question Sheet
Next Lecture Read Chapter 4 (pages 205 – 252) Do Tutorials
4-2 4-4