?
Solved

Creating arrays from multiple numbers in single text box

Posted on 2005-04-18
8
Medium Priority
?
295 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:sonicamps
  • 5
  • 2
8 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 13804377
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
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13804476
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?
0
 

Author Comment

by:sonicamps
ID: 13804521
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13804543
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.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13804581
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
0
 

Author Comment

by:sonicamps
ID: 13804616
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.
0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13804629
TextBox2.Text &= Val(NUM(Int)) & " "

TextBox3.Text &= (Val(NUM_Square(Int))) ^ 2 & " "

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

you're overwriting all the text values... append the text using the &= operator

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

and since you've done most of the work now, i'll point out that you don't really need to sep arrays...

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

        Dim Int As Integer

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

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

remember to give credit to people who have contributed to reaching your solution
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13804637
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question