Link to home
Start Free TrialLog in
Avatar of sonicamps
sonicamps

asked on

Creating arrays from multiple numbers in single text box

I am trying to design a form that meets the following requirements:

1. Read 10 (and only 10) numbers from a textbox and store them in an array (we'll call it NUM).

2. Create another array (we'll call it NUM_Square) to store the square values of the same numbers.

3. Display the contents NUM a different textbox and NUM_Square yet a different textbox.

Normally I would create an array from the values of different textboxes such as:

NUM(9) as Integer
NUM(0) = val(textbox1.txt)
NUM(1) = val(textbox2.txt)

and so on...

But in this instance I have to use the values from a single text box.

Immediate help would be appreciated.
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi sonicamps,

There is are handy functions : Split() and Join() which will achieve this for you:

Dim NUM() As String
NUM = Split(TextBox1.Text," ") ' assuming they are seperated by spaces

You can then create your array for the squares however you like.

To get it back to a string use

TextBox2.Text = Join(NUM," ") ' again seperating by spaces.

Tim Cottee
Avatar of S-Twilley
S-Twilley

I think to make this question easier to answer...  could you let us know what is a valid seperator for the numbers? Only spaces? Commas? .. and how strict is the sepaation... if there were two spaces between numbers... would that cause an error.

Also, can they only be whole numbers... or are fractions allowed?
Avatar of sonicamps

ASKER

Well at this point, I would like to do it with out any spaces allowed if possible.  If that is impossible, then one space will suffice.  The separation is not that strict.  They also can only be whole numbers.
so they're split up into seperate numbers with a comma?

Tim has pointed you in one direction that you can split up your numbers... instead of usign a space in the Split method, you would use a comma.

As for getting the square of a number

Dim i as integer = 3
Dim iSqr As Integer

iSqr = i ^ 2   '  iSqr has the value of 9

=================

we're supposed to point you in the right direction... I feel you should be able to fill in the gaps.
I'll post up an alternative to using the split method in a moment, but as for the intermediate calculations and such, I'll leave that to you.
Here is a more complicated way of checking for validity in the string, and then grabbing the numbers... if you're not too confident with the code below, go with what Tim has suggested... Regular Expressions isn't for everyone! :P

=======================================================

        Dim sRegValid As String = ""
        sRegValid &= "^\s*\d+"
        sRegValid &= "((,|\s+)\d+){9}\s*$"

        Dim sText As String = TextBox1.Text
        Dim regTest As New System.Text.RegularExpressions.Regex(sRegValid)
        Dim thisMatch As Match = regTest.Match(sText)

        'This first testing makes sure the entire string is valid... that it only contains 10 numbers
        'with their seperators

        If thisMatch.Success Then

            'Now we'll grab the actual values since we know the string is all proper

            Dim sNumberFormat As String = "\d+(,|\s+|$)"
            Dim regGrab As New System.Text.RegularExpressions.Regex(sNumberFormat)

            Dim mTenNumbers As System.Text.RegularExpressions.MatchCollection
            mTenNumbers = regGrab.Matches(sText)

            'Double check that we only have 10 numbers
            If mTenNumbers.Count = 10 Then
                Dim thisNumber As Match

                For Each thisNumber In mTenNumbers
                    ' you'll replace this with your calculations and insertion into an array
                    MsgBox(thisNumber.Value)
                Next
            End If
        End If
Thanks for the tip on the split function Tim and S

I suppose it would be best to show the code that I have so far:

Dim NUM() As String = Split(TextBox1.Text, " ")
        Dim NUM_Square() As String = Split(TextBox1.Text, " ")
        Dim Int As Integer

        For Int = 0 To UBound(NUM)
            TextBox2.Text = Val(NUM(Int))
        Next

        For Int = 0 To UBound(NUM_Square)
            TextBox3.Text = (Val(NUM_Square(Int))) ^ 2
        Next

The problem is that when I run this it only displays and squares the last value entered in textbox1.text.  For instance if I enter 3 (space) 5, Textbox2 only shows me "5" and textbox3 only shows me "25".  I can't seem to get to display the value of each number, and the square of each number.  I'm sure i'm going about this all wrong using the For loops.
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
an alternative to the loop to make it look neater could be:

        Dim thisNum As Integer
        For Int = 0 To UBound(NUM)
            thisNum = Integer.Parse(NUM(Int))
            TextBox2.Text &= thisNum & " "
            TextBox3.Text &= (thisNum ^ 2) & " "
        Next