Sign in to follow this  

Help with reading strings!

This topic is 3377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry if this is in the wrong forum, but it's not directly related to games, so i thought I'd post it here. I'm writing a program in VB where the user inputs text with words combined with numbers, and adds together the numbers and outputs it to a text file. For Example, input could look like this (its just an example of the format, not what is actually going into the program): 3:30pm inventory is: *Swords: 34 *Guns: 22 *Cars: 5,732 5:20pm inventory is: *Swords: 24 *Guns: 53 *Cars: 3,344 I need my program to add up all of yesterdays and today's inventories. For example, the output would look like this: Total day's inventory is: *Swords: 58 *Guns: 75 *Cars: 9076 I have my program set up like this. There is a text box for the input (individual inventories) and a button "add" to add the two day's inputs together. Then, the program saves the output (total inventory) to a text file. I just can't figure out how to read only the numbers in the input. For example, getting the program to ignore the letters, asterisks and commas so it can assign each number to its own integer, allowing me to add the integers together to get the output. I know it would be easier if I just put in numbers for the input, but I'm designing this program to make it easy to calculate total "inventories", especially when there 50-100 daily inventories to add together. I designed it this way because I am copying and pasting the: "5:20pm inventory is: *Swords: 24 *Guns: 53 *Cars: 3,344" into the program, and there could be 50-100 of those a day, making it take much longer to just type the numbers in or copy and paste the numbers into the program individually. So basically, I need an easy way to sort of "extract" the integers from a string, ignoring all the letters and punctuation, so I can perform mathematical calculations on them. Sorry for the long post, I hope it's coherent enough to be understood :p. P.S. Please don't say anything like "why are you using VB". Right now, my experience with visual basic in this area is much greater than my experience with C++ and C#. Thanks again.

Share this post


Link to post
Share on other sites
Try this article:

http://msdn.microsoft.com/en-us/library/xbtzcc4w.aspx

You'll have to remove the stuff before the number... if you always include the colon in your string, that should be easy enough.

Good luck!

Share this post


Link to post
Share on other sites
Thanks for the reply. And yes, the colon will always be before the number. So the parse method can convert the number in the string to an integer. That helps.

But now, what do I do to choose where in the string the numbers are when I want to parse and get the integer. For instance, the:
3:30pm inventory is:
*Swords: 34
*Guns: 22
*Cars: 5,732
will all be in one string. This is because the program will have a single text box where that entire log is inputted. The purpose of this program is to extract the numbers from one log and add them to the numbers of another log to create a whole-day total.

Yes, I realize that it seems much easier to just type in the numbers in individual text boxes and omit the other text (like "swords") while inputting to the text box, but that would eliminate the purpose of this program. This program is supposed to make it quicker to take many such logs and add them together (like I said before 50-100 logs per day).

So now, how do I "skip" the label and read only the numbers, or is that a capability of the parse function? And how can I create multiple integers that all come from the same string?

Thanks!

Share this post


Link to post
Share on other sites
From what you have explained the label is an integral part of this process. You cannot ignore it unless the items being added are the same each time. Regardless, you need to start thinking about what you are actually trying to accomplish.

Assuming you don't have to idiot-proof the application this is going to be really easy. Before you start you should outline what your input requirements will be. For example:

All item names will be a single word with no spaces.
The item name will be immediately followed by a colon, a space, and then a number indicating quantity.
There will be a newline between each item name and quantity pair.

This would leave you with something like:

SoupSpoons: 12
RocketCans: 3
Cars: 177
RocketSwordGuns: 42

From a standard format it becomes easy to parse strings like this. You will want to look up the following functions on the MSDN:

Split
IndexOf
Trim

Then use these functions to break the string up into manageable pieces. From there, use the information mumblyjoe gave you to get your actual numbers.

Learn to use the MSDN to find the tools you need to complete whatever task is at hand.

edit: and don't forget trailing quotes in html tags.

Share this post


Link to post
Share on other sites
My apologies, that was a incredibly terse of me.

What you want to do is load your string into a StringReader, then use ReadLine to get at the individual lines. Lastly, use the parsing function mentioned earlier to get the int that follows the colon. It looks line some of your lines won't have an integer on them, so you should be sure to check the output from the parser, so that you know to ignore those lines.

MSDN Docs on String Reader give a nice example of how to use this in VB.NET.

Cheers,

Valere


Share this post


Link to post
Share on other sites
Ahh thanks I really appreciate it. That helped a lot.

BTW: binchawpz, I have been looking on MSDN, but until now I didn't know what to search for. But you just told me (Split, IndexOf, Trim). Like I said, I've never done parsing before. That is what I needed to know. So thanks for giving me the input on what to look up, just please don't lecture me on using MSDN ;P.

Thanks again everyone.

Share this post


Link to post
Share on other sites
That statement was more or less pointing out that a part of learning to use the MSDN is learning how to find what you need, which is not as easy as it should be. For example in this situation you can find everything you need by looking up the String class. It was terse, but I was not intending to offend/demean you.

Regardless, good luck.

Share this post


Link to post
Share on other sites
It's all good. The only problem I have with MSDN is the lack of commenting. Like in the examples for split, there are no comments to explain what is going on in the code, which is making it difficult to learn how to use split.

For example, if I'm understanding this correctly, split will divide a string into different "substrings". Well, how exactly do I access the different substrings. Thanks to Valere, I now have a variable holding one line of the inventory, and I want to use the " " (space) to separate the item name from the amount. Well, after I separate them, how do I assign the second part of the original line string (the amount) to its own string variable, so I can use the parse method to make it an integer?

EDIT: Nevermind, I figured it out! I don't have to use split at all. Instead, I'm using the remove method on the string that holds the entire line to remove the first characters that I don't need. Thanks for everyone's help though.

[Edited by - cemedias on September 16, 2008 9:44:49 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by cemedias
It's all good. The only problem I have with MSDN is the lack of commenting. Like in the examples for split, there are no comments to explain what is going on in the code, which is making it difficult to learn how to use split.


Examples in documentation are meant to show you how to use someone else's code. They aren't meant to teach you how to program.

The example I see is about as simple as it could possibly get:

Quote:
lol example

Dim instance As String
Dim separator As Char()
Dim returnValue As String()

returnValue = instance.Split(separator)


The only comments that could possibly be added to that either (a) assume you don't know what the basic keywords of the language (like 'Dim' and 'As') do, or (b) repeat the description of the function that was already given.

Seriously, what needs explaining here?

Quote:
For example, if I'm understanding this correctly, split will divide a string into different "substrings".


You've understood that much, yes.

It would be pretty hard to misunderstand it, considering the very first sentence of the documentation in question reads:

Quote:
T3h MSDN z0mg!!!11
Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.


So, then, it returns an array of strings, such that those strings are the substrings of the string that you split ("this instance", since it's a member function).

Quote:
Well, how exactly do I access the different substrings.


Well, it returns those substrings in an array. You do know how to access the elements of an array, yes? If so, it should be obvious where to go from here. If not, I don't understand how you got this far.

(Edited a few times because I fail at quote tags.)

Share this post


Link to post
Share on other sites
You don't have to understand how I got this far. You just have to read the question and answer it (if you want to).

Thanks for questioning my capabilities, but in case you didn't notice, I already figured it out, as I stated in my previous post.

Even though something seems simple to you doesn't mean it's simple to everyone else, therefore try to just give helpful advice on the issue at hand rather than putting me down for lack of knowledge in a subject.

All other posts were extremely helpful, and I thank you for your assistance.

Share this post


Link to post
Share on other sites

This topic is 3377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this