Jump to content
  • Advertisement
Sign in to follow this  
cemedias

Help with reading strings!

This topic is 3589 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
Advertisement
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
Sorry if I sound really dumb, but how do I parse each line individually. Before now, I have never done parsing in VB.NET before. 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!