“Green Bean” looking for some clarification/assistance with arrays

billH

New member
Joined
Jul 27, 2005
Messages
1
Location
Virginia
Programming Experience
Beginner
Hello folks,



I have been working with VB.NET for about two months now and have run into a wall. Up to this point I have been able to do everything my professor has asked without too much difficulty. The term is winding up and professor has asked me to work on a couple of problems during the break. One of these problems is to:



- have the user input a number into a text box

- using only integers, display the user’s input in reverse order as a result of a button’s click event

- in addition to reversing the order of the input, I need to display the result in words



Example: User’s input = 1234, I then display 4321 and four three two one



My professor was pleased with the following solution but asked me to try the same problem using arrays:

VB.NET:
[font=Courier New][color=green]'declarations[/color][/font]
 
[font=Courier New][color=blue]Dim[/color] origNum [color=blue]As[/color] [color=blue]Integer[/color][/font]
 
[font=Courier New][color=blue]Dim[/color] revNum [color=blue]As[/color] [color=blue]Integer[/color][/font]
 
[font=Courier New][color=blue]Dim[/color] numWord [color=blue]As[/color] [color=blue]String[/color][/font]
 

 
[font=Courier New][color=blue]Try[/color][/font]
 

 
[font=Courier New][color=green]'assignment[/color][/font]
 
[font=Courier New]origNum = Convert.ToInt32([color=blue]Me[/color].uiInputTextBox.Text)[/font]
 
 
 
[font=Courier New][color=green]'conversion[/color][/font]
 
[font=Courier New][color=blue]Do[/color] [color=blue]While[/color] origNum > 0[/font]
 
[font=Courier New]revNum = (revNum * 10) + (origNum [color=blue]Mod[/color] 10) [/font]
 
[font=Courier New]origNum = origNum \ 10 [/font]
 

 
[font=Courier New][color=blue]Loop[/color][/font]
 

 
[font=Courier New][color=green]'display reversed number[/color][/font]
 
[font=Courier New][color=blue]Me[/color].uiRevInputLabel.Text = revNum.ToString()[/font]
 
 
 
[font=Courier New][color=green]'convert the reversed number to words[/color][/font]
 
[font=Courier New]numWord = [color=blue]Me[/color].uiRevInputLabel.Text[/font]
 
 
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"9"[/color], [color=maroon]"Nine "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"8"[/color], [color=maroon]"Eight "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"7"[/color], [color=maroon]"Seven "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"6"[/color], [color=maroon]"Six "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"5"[/color], [color=maroon]"Five "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"4"[/color], [color=maroon]"Four "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"3"[/color], [color=maroon]"Three "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"2"[/color], [color=maroon]"Two "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"1"[/color], [color=maroon]"One "[/color])[/font]
 
[font=Courier New]numWord = numWord.Replace([color=maroon]"0"[/color], [color=maroon]"Zero "[/color])[/font]
 
 
 
[font=Courier New][color=green]'display reversed number as words[/color][/font]
 
[font=Courier New][color=blue]Me[/color].uiRevWordsLabel.Text = numWord[/font]
 
 
 
[font=Courier New][color=blue]Catch[/color] exFormat [color=blue]As[/color] FormatException[/font]
 
[font=Courier New]MessageBox.Show([color=maroon]"Only integer values allowed."[/color], [color=maroon]"Reverse _[/color][/font]
 
[color=maroon][font=Courier New]Numbers"[/font][/color][font=Courier New], MessageBoxButtons.OK, MessageBoxIcon.Information)[/font]
 
[font=Courier New][color=blue]Catch[/color] ex [color=blue]As[/color] Exception[/font]
 
[font=Courier New]MessageBox.Show(ex.Message, [color=maroon]"Reverse Numbers"[/color], _[/font]
 
[font=Courier New]MessageBoxButtons.OK, MessageBoxIcon.Information)[/font]
 
[font=Courier New][color=blue]End[/color] [color=blue]Try[/color][/font]
 

 
[font=Courier New][color=green]'sends focus to the uiInputTextBox[/color][/font]
 
[font=Courier New][color=blue]Me[/color].uiInputTextBox.Focus()[/font]
[font=Courier New]
[/font]



Now to get to the point… I am unable to figure out how to get the users input into an array. Once I get the input into the array I should be able to use the reverse method to display the results and a select statement to assign and display the output as words. I know this is probably trivial, but if I could solicit any assistance I would greatly appreciate it.


Thanks,

Bill
 
Last edited:
Ok, i'll paste something I made in two minutes. It basically do what you want with some arrays.

VB.NET:
[size=3][color=#0000ff]Dim[/color] s [color=#0000ff]As[/color][color=#0000ff]String[/color] = "12345"[/size]
[size=3] [/size]
[size=3][color=#0000ff]Dim[/color] i(s.Length - 1) [color=#0000ff]As[/color][/size][color=#0000ff][size=3]Integer[/size]
[size=3] [/size]
[/color][size=3][color=#0000ff]Dim[/color] cpt [color=#0000ff]As[/color][/size][color=#0000ff][size=3]Integer[/size]
[size=3] [/size]
[/color][color=#0000ff][size=3]Try[/size]
[size=3] [/size]
[/color][size=3][color=#0000ff]For[/color][color=#0000ff]Each[/color] c [color=#0000ff]As[/color][color=#0000ff]Char[/color][color=#0000ff]In[/color] s[/size]
[size=3] [/size]
[size=3]i(cpt) = [color=#0000ff]Integer[/color].Parse(c)[/size]
[size=3] [/size]
[size=3]cpt += 1[/size]
[size=3] [/size]
[color=#0000ff][size=3]Next[/size]
[size=3] [/size]
[/color][size=3][color=#0000ff]Catch[/color] ex [color=#0000ff]As[/color] Exception[/size]
[size=3] [/size]
[size=3]MessageBox.Show("INTEGERS ONLY")[/size]
[size=3] [/size]
[size=3][color=#0000ff]End[/color][/size][color=#0000ff][size=3]Try[/size]
[size=3] [/size]
[/color][size=3]i.Reverse(i)[/size]
[size=3] [/size]
[size=3][color=#0000ff]Dim[/color] finalString [color=#0000ff]As[/color][/size][color=#0000ff][size=3]String[/size]
[size=3] [/size]
[/color][size=3]cpt = 0[/size]
[size=3] [/size]
[size=3][color=#0000ff]For[/color][color=#0000ff]Each[/color] int [color=#0000ff]As[/color][color=#0000ff]Integer[/color][color=#0000ff]In[/color] i[/size]
[size=3] [/size]
[size=3]finalString += i(cpt).ToString[/size]
[size=3] [/size]
[size=3]cpt += 1[/size]
[size=3] [/size]
[color=#0000ff][size=3]Next[/size]
[size=3] [/size]
[/color][size=1][size=3]MessageBox.Show(finalString)[/size]
[size=3] [/size]
[/size]
 
I think what your professor means is that you should put the words into an array and then you can get the required word simply by supplying the corresponding index. You would create the array like this:
VB.NET:
Private m_NumberWords As String() = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Notice that the word at each index is the word for that number, e.g. "zero" is at index 0.

Edit:
I'm not sure what your requirements are, but it would also be easier to reverse the number when it is in string form rather than converting it to a number first. You could use your own algorithm to reverse it, or you could convert it to an array of characters (String.ToCharArray), reverse it (Array.Reverse) and then turn it back into a string (New String). You could then turn each individual character into a number and use it as an index to get the correct word.

Also, I'm not sure whether you can assume that only valid characters will entered, but if you want to include error checking you could use code like this:
VB.NET:
		Dim valueDbl As Double
		Dim valueInt As Integer

		If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Integer, Nothing, valueDbl) Then
			'A valid integer was entered so convert the double to an integer.
			valueInt = CInt(valueDbl)
		Else
			'An invalid value was entered.
			MessageBox.Show("Please enter only integer values.")
		End If
 
Last edited:
Here's Your Solution

Dim str AsString

Dim rev(10) AsString

Dim i, l, pos AsInteger

PrivateSub Reverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

str = Trim(TextBox1.Text)

l = Len(str):)

pos = l

For i = 1 To l

rev(i - 1) = Mid(str, pos, 1)

pos = pos - 1

Next

For i = 0 To l - 1

Debug.Write(rev(i))

Next

For i = 0 To l - 1

SelectCase (rev(i))

Case 1

Debug.Write("One")

Case 2

Debug.Write("Two")

Case 3

Debug.Write("Three")

Case 4

Debug.Write("Four")

Case 5

Debug.Write("Five")

Case 6

Debug.Write("Six")

Case 7

Debug.Write("Seven")

Case 8

Debug.Write("Eight")

Case 9

Debug.Write("Nine")

Case 10

Debug.Write("Ten")

Case 0

Debug.Write("Zero")

EndSelect

Next

 
Sorry, I couldn't help myself. :eek:
VB.NET:
	[color=Blue]Private[/color] m_NumberWords [color=Blue]As String[/color]() = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}

	[color=Blue]Private Sub[/color] Button1_Click([color=Blue]ByVal[/color] sender [color=Blue]As[/color] System.Object, [color=Blue]ByVal[/color] e [color=Blue]As[/color] System.EventArgs) [color=Blue]Handles[/color] Button1.Click
		[color=Blue][/color][color=Green]'Check whether the input is valid.[/color]
		[color=Blue]If Not Double[/color].TryParse([color=Blue]Me[/color].uiInputTextBox.Text, Globalization.NumberStyles.Integer, [color=Blue]Nothing[/color], [color=Blue]New Double[/color]) [color=Blue]Then[/color]
			MessageBox.Show("Please enter an integer value.")
			[color=Blue]Return[/color]
		[color=Blue]End If[/color]

		[color=Green]'Get the input characters.[/color]
		[color=Blue]Dim[/color] inputChars [color=Blue]As Char[/color]() = [color=Blue]Me[/color].uiInputTextBox.Text.ToCharArray()

		[color=Green]'Reverse the input characters.[/color]
		Array.Reverse(inputChars)

		[color=Green]'Display the reversed characters.[/color]
		[color=Blue]Me[/color].uiRevInputLabel.Text = [color=Blue]New String[/color](inputChars)

		[color=Blue]Dim[/color] outputWords [color=Blue]As String[/color]

		[color=Green]'Get the word for each digit.[/color]
		[color=Blue]For Each[/color] numChar [color=Blue]As Char In[/color] inputChars
			[color=Green]'Add a space between each pair of words.[/color]
			[color=Blue]If[/color] outputWords <> [color=Blue]String[/color].Empty [color=Blue]Then[/color]
				outputWords &= " "
			[color=Blue]End If[/color]

			[color=Green]'Add the word at the index corresponding to the digit.[/color]
			outputWords &= [color=Blue]Me[/color].m_NumberWords([color=Blue]CInt[/color](numChar.ToString()))
		[color=Blue]Next[/color] numChar

		[color=Green]'Display the number in words.[/color]
		[color=Blue]Me[/color].uiRevWordsLabel.Text = outputWords
	[color=Blue]End Sub[/color]
 
Back
Top