473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

VB.NET App: Ripping apart Strings

Ok guys, This is a quick overview of what I'm trying to do. 2 textboxes. textbox1 has a fraction entered by the user into it in one of these 2 ways
1(space)3/4 or simply 3/4
Now what this code is supposed to do is get the length of the entry ans tear it apart storing any numbers found in the string. These are later used to turn the fraction entered into a ecimal equivelent. (Note) i know I can do this with 3 boxes by having a whole #,numerator, and denominator box I allready have. But I would like to have only 1 box for the entry. Below is what I have come up with so far (with notations) But it doesn't seem to be reading the string at all. when i run it I get "NaN" in textbox2. Any ideas would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1.     ' on button click event
  2.       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.         ' setting my variables
  4.         ' gets the length of the entry in textbox1
  5.         Dim GetLength As Integer = TextBox1.Text.Length
  6.         ' used later as the starting place when breaking up the string
  7.         Dim Textstart As Integer
  8.         ' a paece of the string
  9.         Dim r As String
  10.         ' integer storage
  11.         Dim z As Integer
  12.         Dim a As Integer
  13.         Dim b As Integer
  14.         ' parts of the fractions
  15.         Dim WholeNumber As Integer
  16.         Dim Numerator As Integer
  17.         Dim Denominator As Integer
  18.         ' answer
  19.         Dim DecimalNumber As Double
  20.         ' used with r as a temp storage of any integer found
  21.         Dim x As Integer
  22.  
  23.         ' loop to shrink the string from right to left till only an integer remains_
  24.         ' or getlength = 0
  25.         Do Until GetLength = 0 Or (IsNumeric(x)) = True
  26.             ' starts by looking at the whole string and chops 1 space off for every time r_
  27.             ' cant be converted to x
  28.             r = r.Substring(1, (GetLength))
  29.             MsgBox(r)
  30.             x = r
  31.             MsgBox(x)
  32.             'subtracts 1 from GetLength
  33.             GetLength -= 1
  34.             MsgBox(GetLength)
  35.             ' takes the string and places it as r
  36.             r.ToString()
  37.             MsgBox(r)
  38.             ' makes x = r to use in loop check
  39.             x = r
  40.             MsgBox(x)
  41.             ' makes a the final integer when the loop finishes
  42.             z = x
  43.             MsgBox(a)
  44.         Loop
  45.         ' adds 2 to the start position to acount for a space or / 
  46.         Textstart = (GetLength + 2)
  47.  
  48.         ' Reruns the loop with a new start point everything inside is the same as above loop
  49.         Do Until GetLength = 0 Or IsNumeric(x) = True
  50.  
  51.             r = r.Substring(Textstart, GetLength)
  52.             MsgBox(r)
  53.             GetLength -= 1
  54.             MsgBox(GetLength)
  55.             r.ToString()
  56.             MsgBox(r)
  57.             x = r
  58.             MsgBox(x)
  59.             a = x
  60.             MsgBox(a)
  61.  
  62.         Loop
  63.  
  64.  
  65.         ' again adds 2 to the start point
  66.         Textstart = (GetLength + 2)
  67.  
  68.         ' runs the loop the final time
  69.         Do Until GetLength = 0 Or IsNumeric(x) = True
  70.  
  71.             r = r.Substring(Textstart, GetLength)
  72.             MsgBox(r)
  73.             GetLength -= 1
  74.             MsgBox(GetLength)
  75.             r.ToString()
  76.             MsgBox(r)
  77.             x = r
  78.             MsgBox(x)
  79.             b = x
  80.             MsgBox(b)
  81.  
  82.         Loop
  83.  
  84.         ' Sets the whole # numerator and denominator  based on 2 numbers_
  85.         ' or only a numerator and denominator are present
  86.         If b = Nothing Or 0 Then
  87.             Numerator = (z)
  88.             MsgBox(z)
  89.             Denominator = (a)
  90.             MsgBox(a)
  91.             WholeNumber = 0
  92.             ' otherwise sets them based on 3 integers found
  93.         Else
  94.             WholeNumber = (z)
  95.             Numerator = (a)
  96.             Denominator = (b)
  97.             ' does the math
  98.         End If
  99.         DecimalNumber = ((Numerator / Denominator) + (WholeNumber))
  100.         TextBox2.Text = "" & (DecimalNumber)
  101.  
  102.     End Sub
  103.  
  104.  
[/vb]
Dec 13 '07 #1
2 1660
Ok guys, This is a quick overview of what I'm trying to do. 2 textboxes. textbox1 has a fraction entered by the user into it in one of these 2 ways
1(space)3/4 or simply 3/4
Now what this code is supposed to do is get the length of the entry ans tear it apart storing any numbers found in the string. These are later used to turn the fraction entered into a ecimal equivelent. (Note) i know I can do this with 3 boxes by having a whole #,numerator, and denominator box I allready have. But I would like to have only 1 box for the entry. Below is what I have come up with so far (with notations) But it doesn't seem to be reading the string at all. when i run it I get "NaN" in textbox2. Any ideas would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1.  [vb]
  2.     ' on button click event
  3.       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         ' setting my variables
  5.         ' gets the length of the entry in textbox1
  6.         Dim GetLength As Integer = TextBox1.Text.Length
  7.         ' used later as the starting place when breaking up the string
  8.         Dim Textstart As Integer
  9.         ' a paece of the string
  10.         Dim r As String
  11.         ' integer storage
  12.         Dim z As Integer
  13.         Dim a As Integer
  14.         Dim b As Integer
  15.         ' parts of the fractions
  16.         Dim WholeNumber As Integer
  17.         Dim Numerator As Integer
  18.         Dim Denominator As Integer
  19.         ' answer
  20.         Dim DecimalNumber As Double
  21.         ' used with r as a temp storage of any integer found
  22.         Dim x As Integer
  23.  
  24.         ' loop to shrink the string from right to left till only an integer remains_
  25.         ' or getlength = 0
  26.         Do Until GetLength = 0 Or (IsNumeric(x)) = True
  27.             ' starts by looking at the whole string and chops 1 space off for every time r_
  28.             ' cant be converted to x
  29.             r = r.Substring(1, (GetLength))
  30.             MsgBox(r)
  31.             x = r
  32.             MsgBox(x)
  33.             'subtracts 1 from GetLength
  34.             GetLength -= 1
  35.             MsgBox(GetLength)
  36.             ' takes the string and places it as r
  37.             r.ToString()
  38.             MsgBox(r)
  39.             ' makes x = r to use in loop check
  40.             x = r
  41.             MsgBox(x)
  42.             ' makes a the final integer when the loop finishes
  43.             z = x
  44.             MsgBox(a)
  45.         Loop
  46.         ' adds 2 to the start position to acount for a space or / 
  47.         Textstart = (GetLength + 2)
  48.  
  49.         ' Reruns the loop with a new start point everything inside is the same as above loop
  50.         Do Until GetLength = 0 Or IsNumeric(x) = True
  51.  
  52.             r = r.Substring(Textstart, GetLength)
  53.             MsgBox(r)
  54.             GetLength -= 1
  55.             MsgBox(GetLength)
  56.             r.ToString()
  57.             MsgBox(r)
  58.             x = r
  59.             MsgBox(x)
  60.             a = x
  61.             MsgBox(a)
  62.  
  63.         Loop
  64.  
  65.  
  66.         ' again adds 2 to the start point
  67.         Textstart = (GetLength + 2)
  68.  
  69.         ' runs the loop the final time
  70.         Do Until GetLength = 0 Or IsNumeric(x) = True
  71.  
  72.             r = r.Substring(Textstart, GetLength)
  73.             MsgBox(r)
  74.             GetLength -= 1
  75.             MsgBox(GetLength)
  76.             r.ToString()
  77.             MsgBox(r)
  78.             x = r
  79.             MsgBox(x)
  80.             b = x
  81.             MsgBox(b)
  82.  
  83.         Loop
  84.  
  85.         ' Sets the whole # numerator and denominator  based on 2 numbers_
  86.         ' or only a numerator and denominator are present
  87.         If b = Nothing Or 0 Then
  88.             Numerator = (z)
  89.             MsgBox(z)
  90.             Denominator = (a)
  91.             MsgBox(a)
  92.             WholeNumber = 0
  93.             ' otherwise sets them based on 3 integers found
  94.         Else
  95.             WholeNumber = (z)
  96.             Numerator = (a)
  97.             Denominator = (b)
  98.             ' does the math
  99.         End If
  100.         DecimalNumber = ((Numerator / Denominator) + (WholeNumber))
  101.         TextBox2.Text = "" & (DecimalNumber)
  102.  
  103.     End Sub
  104.  
  105.  
[/vb]
nevermind guys i found a way to do it from another angle using "IndexOf"

Here's what I wound up with
Expand|Select|Wrap|Line Numbers
  1.  
  2. ' on button click event
  3.       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         ' Set my variables
  5.         Dim WholeNumber As Integer
  6.         Dim Numerator As Integer
  7.         Dim Denominator As Integer
  8.         Dim StoreVariable1 As Integer
  9.         Dim StoreVariable2 As Integer
  10.         Dim StoreVariable3 As Integer
  11.         Dim space1 As Integer
  12.         Dim slash1 As Integer
  13.         Dim DecimalNumber As Double
  14.  
  15.         ' look for the space in the entry
  16.         space1 = TextBox1.Text.IndexOf(" ")
  17.  
  18.         ' runs if there is no space found
  19.         If Not (space1 >= 0) Then
  20.  
  21.             'looks for a slash in the entry
  22.             slash1 = TextBox1.Text.IndexOf("/")
  23.             ' stores what it finds from the beginning to the slash as a number
  24.             StoreVariable2 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  25.             'looks at everything after the slash then stores that as a number
  26.             StoreVariable3 = TextBox1.Text.Substring(slash1 + 1)
  27.  
  28.             ' does the same as the if not but only when a space is found
  29.         Else
  30.  
  31.  
  32.             StoreVariable1 = TextBox1.Text.Substring(0, space1)
  33.  
  34.             slash1 = TextBox1.Text.IndexOf("/")
  35.  
  36.             StoreVariable2 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  37.  
  38.             StoreVariable3 = TextBox1.Text.Substring(slash1 + 1)
  39.  
  40.         End If
  41.         ' this if else places the stored values into the proper place and does appropriate math
  42.         If (space1) = Nothing Or (space1 <= 0) Then
  43.  
  44.             Numerator = StoreVariable2
  45.             Denominator = StoreVariable3
  46.             DecimalNumber = (Numerator / Denominator)
  47.             TextBox2.Text = (DecimalNumber)
  48.  
  49.         Else
  50.  
  51.             WholeNumber = StoreVariable1
  52.             Numerator = StoreVariable2
  53.             Denominator = StoreVariable3
  54.             DecimalNumber = ((Numerator / Denominator) + (WholeNumber))
  55.             TextBox2.Text = (DecimalNumber)
  56.  
  57.         End If
  58.  
  59.     End Sub
  60.  
It's probably over coded so going to try to clean it up. Works perfectly though.
Dec 14 '07 #2
nevermind guys i found a way to do it from another angle using "IndexOf"

Here's what I wound up with
Expand|Select|Wrap|Line Numbers
  1.  
  2. ' on button click event
  3.       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         ' Set my variables
  5.         Dim WholeNumber As Integer
  6.         Dim Numerator As Integer
  7.         Dim Denominator As Integer
  8.         Dim StoreVariable1 As Integer
  9.         Dim StoreVariable2 As Integer
  10.         Dim StoreVariable3 As Integer
  11.         Dim space1 As Integer
  12.         Dim slash1 As Integer
  13.         Dim DecimalNumber As Double
  14.  
  15.         ' look for the space in the entry
  16.         space1 = TextBox1.Text.IndexOf(" ")
  17.  
  18.         ' runs if there is no space found
  19.         If Not (space1 >= 0) Then
  20.  
  21.             'looks for a slash in the entry
  22.             slash1 = TextBox1.Text.IndexOf("/")
  23.             ' stores what it finds from the beginning to the slash as a number
  24.             StoreVariable2 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  25.             'looks at everything after the slash then stores that as a number
  26.             StoreVariable3 = TextBox1.Text.Substring(slash1 + 1)
  27.  
  28.             ' does the same as the if not but only when a space is found
  29.         Else
  30.  
  31.  
  32.             StoreVariable1 = TextBox1.Text.Substring(0, space1)
  33.  
  34.             slash1 = TextBox1.Text.IndexOf("/")
  35.  
  36.             StoreVariable2 = TextBox1.Text.Substring((space1 + 1), (slash1 - (space1 + 1)))
  37.  
  38.             StoreVariable3 = TextBox1.Text.Substring(slash1 + 1)
  39.  
  40.         End If
  41.         ' this if else places the stored values into the proper place and does appropriate math
  42.         If (space1) = Nothing Or (space1 <= 0) Then
  43.  
  44.             Numerator = StoreVariable2
  45.             Denominator = StoreVariable3
  46.             DecimalNumber = (Numerator / Denominator)
  47.             TextBox2.Text = (DecimalNumber)
  48.  
  49.         Else
  50.  
  51.             WholeNumber = StoreVariable1
  52.             Numerator = StoreVariable2
  53.             Denominator = StoreVariable3
  54.             DecimalNumber = ((Numerator / Denominator) + (WholeNumber))
  55.             TextBox2.Text = (DecimalNumber)
  56.  
  57.         End If
  58.  
  59.     End Sub
  60.  
It's probably over coded so going to try to clean it up. Works perfectly though
Dec 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: john67 | last post by:
The company I work for is about to embark on developing a commercial application that will cost us tens-of-millions to develop. When all is said and done it will have thousands of business...
25
by: Dave Turner | last post by:
I know that its impossible to completely prevent somebody from ripping a site (or cracking software) if that person has the skills and the time/patience, but there are tricks that can be employed...
4
by: mt | last post by:
Hi, W H A T I am considering moving my windows app written in Visual C++ 6.0 to C# .NET. Q U E S T I O N I was wondering if application speed will be a problem in .NET when compared to a...
22
by: Deano | last post by:
Hi, I have a finished Microsoft Access app that we are distributing using an Access runtime. This works fine (mostly) but I'm sold on the advantages of dot.NET and upgrading to vb.NET seems...
7
by: noridotjabi | last post by:
I'm working on a prompt style thing and I need a way to pass arguments to commands within my program. For example: ::hello -v -r -n If this is entered as one input string, can I (in any...
1
by: M O J O | last post by:
Hi, I've have searched google, but can't find a solution to my problem. Om my develloper machine, I use one app.config, but when I deploy, I need to deploy another app.config. The reason...
3
by: =?Utf-8?B?QnJhZA==?= | last post by:
im trying to create a vfp app that can talk to a vb 2005 app to launch forms basically, i want to tell the vfp app to launch a form when they select an option from the vb menu that hasnt been...
2
by: PromisedOyster | last post by:
We have an executable that uses an App.Config file to hold things such as database connection strings etc. We now want to write a wrapper DLL around that executable to expose functionality to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.