2

I'm making a program that will give you the result of a sum when the input is unstructured. Please see the following:

Input:

whats 20 + 26 

Output:

46 

I already know how to search for text in a string (If text.IndexOf("hello") >= 0 Then)...

I have no idea how to use the two integers for an addition equation.

9
  • 1
    What's the purpose for using an unstructured process of inputting data? I.e: Why the added complication of needing to parse through input as opposed to a requested value 1, value 2, value n? Commented Jan 4, 2014 at 2:39
  • 2
    This is actually a rather difficult problem. It is not a specific programming question, it is a broad programming problem. To solve it you need to apply a very broad set of programming tools, especially if you have no restrictions on the format of the input string. It's clear you haven't even thought through all of the sub-problems that are required to put this together. What if a user types what is 20 + 26 or I have 20 apples and 26 fingers or whatever you do, don't tell me the sum of 20 + 26, or What are 26.9+7 things % 133t k0derz can d0? Commented Jan 4, 2014 at 2:57
  • 1
    In brief, consider the complexity of what you are asking and try to break the problem down into simpler subsets. As it stands, this question is asking how to write a natural language parser - this is not a trivial problem. If you're just learning how to program, I suggest you start with the basics first. Commented Jan 4, 2014 at 2:58
  • You might want to start looking at regular expressions - questions like this might help : stackoverflow.com/q/3373885/327083 Mind you, regular expressions are only going to provide a limited and specific type of solution to this problem, but it's at least a start. Commented Jan 4, 2014 at 3:01
  • just add restrictions to your textbox - like no alphabets allowed. that will simplify your homework :) also, what kind of a person will type "what's 20+20" rather than "20+20" clicks button Commented Jan 4, 2014 at 3:14

2 Answers 2

0

Here's a quick example of how to start doing this with regular expressions. This won't be bulletproof against anything you throw at it, but it should make for a good start.

Be sure to add : Imports System.Text.RegularExpressions to your .vb file

 'This string is an example input. It demonstrates that the method below 'will find the sum "2345+ 3256236" but will skip over things like ' if + .04g ' 1.23 + 4 ' etc... Dim input As String = _ "aoe%rdes 2345+ 3256236 if + .04g rcfo 8 3 . 1.23 + 4 the#r whuts" Dim pattern As String = "\s\d+\s*\+\s*\d+(?=\s|$)" For Each _match As Match In Regex.Matches(input, pattern) Dim a = _match.Value.Split("+"c) 'Match extracts "2345+ 3256325" Dim x As Integer Dim y As Integer If Integer.TryParse(a(0), x) AndAlso Integer.TryParse(a(1), y) Then Console.WriteLine("Found the Sum : " & x & " + " & y) Console.WriteLine("Sum is : " & x + y) Else Console.WriteLine("Match failed to parse") End If Next 

The regular expression can be broken down as

\s '(definitely) one whitespace \d+ 'followed by any number of integer digits \s* 'followed by (possibly) a whitespace \+ 'followed by (definitely) a "+" \s* 'followed by (possibly) a whitespace \d+ 'followed by any number of integer digits (?=\s|$) 'followed by (definitely) either a whitespace or EOL 

Read more here :

Regular Expression Language - Quick Reference

.NET Framework Regular Expressions

Regular Expressions Reference

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I was busy with other code.I just came back to my question. Ok either i really suck at vb.net or you did it wrong. I tried your code (i did not really understand it) and it did not work. I typed in whats 6 + 6 and it gave me a long string of integers 3258581 which im pretty sure is not the answer to 6 + 6. SO YEAH I PROBABLY DID SOMETHING WRONG.
@Naqeeb 3258581 is the answer to the example string input in my answer. The idea is that you are supposed to replace input with your input. You can't just close your eyes, press ctrl+c, ctrl+v and hope that things are going to work. This is programming, son. You've got to read and pay attention. Did you not notice the code also output Found the Sum : 2345 + 3256236 - which is the same sum that is in the string input?
Ok sorry ill try it again, like I said i did not really understand it :(
@Naqeeb did you read the links I posted? To understand the answer you have to understand what regular expressions are and how they work. The links at the end should give you a good introduction - the rest should make more sense after that.
-1

Parse the two integers into two integer variables - we'll call them x and y. Then add their results.

Dim x as Integer Dim y as Integer Dim result as Integer 'parse your integers- you indicate you already know how to do this 'add the results result = x + y 'now print out the result 

5 Comments

He did not say he already knew how to parse the integers - he said he knew how to find specific text, not general things like an unspecified integer of arbitrary size.
@J... Please note that the OP made two separate statements that indicate that he can most likely parse the integers: 1) "I already know how to search for text in a string"; and 2) "I have no idea how to use the two integers for an addition equation". How can he have two integers "for an addition equation" if they haven't been parsed? I answered his question of "I have no idea how to use the two integers for an addition equation". Your down-votes of my and Dan Verdolino 's responses are unmerited. I hope you have a good day.
Read the post title - it asks How to search for an integer in a string. The statement "I know how to search for text" does not mean "I know how to search for integers". English is obviously not OP's native language - statement 2 most logically is to be read as "I have no idea how to use the two integers [in the string] for an addition equation". Surely OP is not stumped at the syntax for assignment and addition. Think about it and look at what actual content you've provided.
thanks for giving an anwer but i need to find 2 integers in the text from the textbox.
@J... I honestly have no idea what the OP's native language is. I was simply trying to help her/him. How you can assume that he well knows one thing and not another is beyond me. Clairvoyance, perhaps. Naqeeb, as others have said, parsing of two separate integers from a string can be difficult without placing stringent rules on the format of that string. You may want to look into the subject of lexical analysis and parsing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.