Problem with string manipulation or calling array with string manipulation

  • Thread starter Thread starter Van wilderer
  • Start date Start date
V

Van wilderer

Guest
I have to write a program that does the following:

This project is going to require two arrays. The first will contain a list of the following cities:
Canton, Cincinnati, Cleveland, Columbus, Dayton, Toledo, Youngstown
This array will take the form:
Dim cities() As String = {"Apple Creek”, "Orrville", "Wooster"}
The program should test each user entry against the above array to make sure the user is entering a valid city. If the city entered is not in the array, ask the user form a valid entry.
Since string comparisons are literal, you are to add a procedure that will convert the user entry into the appropriate form (first letter of the city uppercase, all remaining letters lower case).
The second array is a 2 dimensional array will hold the distance from one city to another city. Loading the array is going to require some special design considerations. The array table (elements) is arranged as a mirror image along an axis that runs from top left to bottom right. This makes logical sense as the distance from say Orrville to Wooster is the same as the distance from Wooster to Orrville (see below). Also the axis elements must be equal to zero (the distance from Orrville to Orrville equals 0).
Apple Creek Orrville Wooster
Apple Creek 0 13 17
Orrville 13 0 11
Wooster 17 11 0
We will be using a random number generator to load the array with integers (see page 298). This program will require a nested loop in conjunction with the random number generator to load the two dimensional array with values. You
will need to use the loop control variables as the array subscripts (addresses) to cycle you through the array and assign values to the elements.
Don’t worry that the array elements don’t reflect the actual mileage between the cities. The important objective is to generate the random numbers and load them into the array such that the array is the mirror image form that was discussed above.
The test to determine if your program works correctly is being able to enter two city names, return a value (miles) and then reverse the order of the cities entered and return the same value (miles). Also, if you enter the same city twice it should return a zero.

However, the following lines are giving me problem either because of how Im calling the array or because of my use of string manipulation:

Dim StartFirstCityNameFirstInit As String = startcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim StartFirstCityNameAfterFirstInit As String = startcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim DestFirstCityNameFirstInit As String = destcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim DestFirstCityNameAfterFirstInit As String = destcityNames(0).strInput.Tolower Like "[A-Z]*"

and in the other if statement these following:

Dim StartFirstCityNameFirstInit As String = startcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim StartFirstCityNameAfterFirstInit As String = startcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim StartSecondCityNameFirstInit As String = startcityNames(1).strInput.ToUpper Like "[A-Z]"
Dim StartSecondCityNameAfterFirstInit As String = startcityNames(1).strInput.Tolower Like "[A-Z]*"
Dim DestFirstCityNameFirstInit As String = destcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim DestFirstCityNameAfterFirstInit As String = destcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim DestSecondCityNameFirstInit As String = destcityNames(1).strInput.ToUpper Like "[A-Z]"
Dim DestSecondCityNameAfterFirstInit As String = destcityNames(1).strInput.Tolower Like "[A-Z]*"


Im getting the following errors currently:

Warning 3 Unused local variable: StartSecondCityNameFirstInit. 9 13 Program9
Warning 4 Unused local variable: StartSecondCityNameAfterFirstInit. 10 13 Program9
Warning 1 Unused local variable: StartFirstCityNameFirstInit. 7 13 Program9
Warning 2 Unused local variable: StartFirstCityNameAfterFirstInit. 8 13 Program9
Warning 7 Unused local variable: DestSecondCityNameFirstInit. 13 13 Program9
Warning 8 Unused local variable: DestSecondCityNameAfterFirstInit. 14 13 Program9
Warning 5 Unused local variable: DestFirstCityNameFirstInit. 11 13 Program9
Warning 6 Unused local variable: DestFirstCityNameAfterFirstInit. 12 13 Program9
Error 9 Number of indices is less than the number of dimensions of the indexed array. 53 36 Program9
Error 10 Number of indices is less than the number of dimensions of the indexed array. 53 74 Program9
Error 11 strInput is not a member of String. 64 57 Program9
Error 12 strInput is not a member of String. 65 62 Program9
Error 13 strInput is not a member of String. 66 56 Program9
Error 14 strInput is not a member of String. 67 61 Program9
Error 15 strInput is not a member of String. 86 57 Program9
Error 16 strInput is not a member of String. 87 62 Program9
Error 17 strInput is not a member of String. 88 58 Program9
Error 18 strInput is not a member of String. 89 63 Program9
Error 19 strInput is not a member of String. 90 56 Program9
Error 20 strInput is not a member of String. 91 61 Program9
Error 21 strInput is not a member of String. 92 57 Program9
Error 22 strInput is not a member of String. 93 62 Program9


The following errors Im not sure why because I have the variables declared and initialized as follows:

Warning 3 Unused local variable: StartSecondCityNameFirstInit. 9 13 Program9
Warning 4 Unused local variable: StartSecondCityNameAfterFirstInit. 10 13 Program9
Warning 1 Unused local variable: StartFirstCityNameFirstInit. 7 13 Program9
Warning 2 Unused local variable: StartFirstCityNameAfterFirstInit. 8 13 Program9
Warning 7 Unused local variable: DestSecondCityNameFirstInit. 13 13 Program9
Warning 8 Unused local variable: DestSecondCityNameAfterFirstInit. 14 13 Program9
Warning 5 Unused local variable: DestFirstCityNameFirstInit. 11 13 Program9
Warning 6 Unused local variable: DestFirstCityNameAfterFirstInit. 12 13 Program9


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cities() As String = {"Canton", "Cincinnati", "Cleveland", "Columbus", "Dayton", "Toledo", "Youngstown"}
Dim StartFirstCityNameFirstInit As Char
Dim StartFirstCityNameAfterFirstInit As String
Dim StartSecondCityNameFirstInit As Char
Dim StartSecondCityNameAfterFirstInit As String
Dim DestFirstCityNameFirstInit As Char
Dim DestFirstCityNameAfterFirstInit As String
Dim DestSecondCityNameFirstInit As Char
Dim DestSecondCityNameAfterFirstInit As String


If startcityNames(2).Count = 1 Then
MessageBox.Show("City name is one word")
Dim StartFirstCityNameFirstInit As String = startcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim StartFirstCityNameAfterFirstInit As String = startcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim DestFirstCityNameFirstInit As String = destcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim DestFirstCityNameAfterFirstInit As String = destcityNames(0).strInput.Tolower Like "[A-Z]*"
txtStart.Text = String.Empty
txtDestination.Text = String.Empty
btnFindMilage.Enabled = True
txtStart.Focus()
txtStart.Text = StartFirstCityNameFirstInit & StartFirstCityNameAfterFirstInit
txtDestination.Text = DestFirstCityNameFirstInit & DestFirstCityNameAfterFirstInit
Do While txtStart.Text = txtDestination.Text
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
Do While txtStart.Text = txtDestination.Text
cities.Reverse()
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
Exit Sub
End If
If startcityNames(2).Count = 2 Then
Dim StartFirstCityNameFirstInit As String = startcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim StartFirstCityNameAfterFirstInit As String = startcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim StartSecondCityNameFirstInit As String = startcityNames(1).strInput.ToUpper Like "[A-Z]"
Dim StartSecondCityNameAfterFirstInit As String = startcityNames(1).strInput.Tolower Like "[A-Z]*"
Dim DestFirstCityNameFirstInit As String = destcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim DestFirstCityNameAfterFirstInit As String = destcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim DestSecondCityNameFirstInit As String = destcityNames(1).strInput.ToUpper Like "[A-Z]"
Dim DestSecondCityNameAfterFirstInit As String = destcityNames(1).strInput.Tolower Like "[A-Z]*"
txtStart.Text = String.Empty
txtDestination.Text = String.Empty
btnFindMilage.Enabled = True
txtStart.Focus()
txtStart.Text = StartFirstCityNameFirstInit & StartFirstCityNameAfterFirstInit & " " & StartSecondCityNameFirstInit & StartSecondCityNameAfterFirstInit
txtDestination.Text = DestFirstCityNameFirstInit & DestFirstCityNameAfterFirstInit & " " & DestSecondCityNameFirstInit & DestSecondCityNameAfterFirstInit
Do While txtStart.Text = txtDestination.Text
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
Do While txtStart.Text = txtDestination.Text
cities.Reverse()
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
End If


A picture of an example of the form designer can be seen here:

b2026e204dfffb900b271f75ac1a4594._.png



My entire source code is as follows and is pretty clean at the moment, but will it do what its supposed to do once the above errors are corrected as well though.

Option Strict On
Imports System.Text.RegularExpressions
Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cities() As String = {"Canton", "Cincinnati", "Cleveland", "Columbus", "Dayton", "Toledo", "Youngstown"}
Dim StartFirstCityNameFirstInit As Char
Dim StartFirstCityNameAfterFirstInit As String
Dim StartSecondCityNameFirstInit As Char
Dim StartSecondCityNameAfterFirstInit As String
Dim DestFirstCityNameFirstInit As Char
Dim DestFirstCityNameAfterFirstInit As String
Dim DestSecondCityNameFirstInit As Char
Dim DestSecondCityNameAfterFirstInit As String
End Sub

Private Sub lblStart_Click(sender As Object, e As EventArgs) Handles lblStart.Click

End Sub

Private Sub lblDestination_Click(sender As Object, e As EventArgs) Handles lblDestination.Click

End Sub

Private Sub lblMilage_Click(sender As Object, e As EventArgs) Handles lblMilage.Click

End Sub

Private Sub txtStart_TextChanged(sender As Object, e As EventArgs) Handles txtStart.TextChanged

End Sub

Private Sub txtDestination_TextChanged(sender As Object, e As EventArgs) Handles txtDestination.TextChanged

End Sub

Private Sub txtMilage_TextChanged(sender As Object, e As EventArgs) Handles txtMilage.TextChanged

End Sub

Private Sub btnFindMilage_Click(sender As Object, e As EventArgs) Handles btnFindMilage.Click
Dim cities() As String = {"Canton", "Cincinnati", "Cleveland", "Columbus", "Dayton", "Toledo", "Youngstown"}
txtStart.Focus()
Dim startcityNames() As String = txtStart.Text.Split(" ".ToCharArray, 2, StringSplitOptions.RemoveEmptyEntries)
Dim destcityNames() As String = txtDestination.Text.Split(" ".ToCharArray, 2, StringSplitOptions.RemoveEmptyEntries)
Dim startcityNames() As String = txtStart.Text.Split(" ".ToString("2"), StringSplitOptions.RemoveEmptyEntries) tried because above was thought to be incorrect
Dim destcityNames() As String = txtDestination.Text.Split(" ".ToString(2), StringSplitOptions.RemoveEmptyEntries) tried because above was thought to be incorrect
If txtStart.Text = txtDestination.Text Then
txtMilage.Text = CStr(0)
MessageBox.Show("Must enter different start and destinations")
End If
If (txtStart.Text <> cities) And (txtDestination.Text <> cities) Then
If (txtStart.Text <> cities()) And (txtDestination.Text <> cities()) Then
If txtStart.Text And txtDestination.Text <> cities Then
MessageBox.Show("Must enter Canton, Cincinnati, Cleveland, Columbus, Dayton, Toledo, or Youngstown")
Comment this if statment and cities array declaration out to work with any city
End If
If startcityNames(1).Count < 1 Then
MessageBox.Show("Must have at least a one word name")
Exit Sub
End If
If startcityNames(2).Count = 1 Then
MessageBox.Show("City name is one word")
Dim StartFirstCityNameFirstInit As String = startcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim StartFirstCityNameAfterFirstInit As String = startcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim DestFirstCityNameFirstInit As String = destcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim DestFirstCityNameAfterFirstInit As String = destcityNames(0).strInput.Tolower Like "[A-Z]*"
txtStart.Text = String.Empty
txtDestination.Text = String.Empty
btnFindMilage.Enabled = True
txtStart.Focus()
txtStart.Text = StartFirstCityNameFirstInit & StartFirstCityNameAfterFirstInit
txtDestination.Text = DestFirstCityNameFirstInit & DestFirstCityNameAfterFirstInit
Do While txtStart.Text = txtDestination.Text
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
Do While txtStart.Text = txtDestination.Text
cities.Reverse()
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
Exit Sub
End If
If startcityNames(2).Count = 2 Then
Dim StartFirstCityNameFirstInit As String = startcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim StartFirstCityNameAfterFirstInit As String = startcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim StartSecondCityNameFirstInit As String = startcityNames(1).strInput.ToUpper Like "[A-Z]"
Dim StartSecondCityNameAfterFirstInit As String = startcityNames(1).strInput.Tolower Like "[A-Z]*"
Dim DestFirstCityNameFirstInit As String = destcityNames(0).strInput.ToUpper Like "[A-Z]"
Dim DestFirstCityNameAfterFirstInit As String = destcityNames(0).strInput.Tolower Like "[A-Z]*"
Dim DestSecondCityNameFirstInit As String = destcityNames(1).strInput.ToUpper Like "[A-Z]"
Dim DestSecondCityNameAfterFirstInit As String = destcityNames(1).strInput.Tolower Like "[A-Z]*"
txtStart.Text = String.Empty
txtDestination.Text = String.Empty
btnFindMilage.Enabled = True
txtStart.Focus()
txtStart.Text = StartFirstCityNameFirstInit & StartFirstCityNameAfterFirstInit & " " & StartSecondCityNameFirstInit & StartSecondCityNameAfterFirstInit
txtDestination.Text = DestFirstCityNameFirstInit & DestFirstCityNameAfterFirstInit & " " & DestSecondCityNameFirstInit & DestSecondCityNameAfterFirstInit
Do While txtStart.Text = txtDestination.Text
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
Do While txtStart.Text = txtDestination.Text
cities.Reverse()
Dim randGen As New Random
txtMilage.Text = CStr(randGen.Next(1, 50))
Loop
End If
End Sub
End Class

Continue reading...
 
Back
Top