Need to count integers contained in a string

Here is the string:

xxx%xxx%xx*6*9*1111111*4#xxx*4*9*2222222*3%2121212122$xxxx%xxxxxxxxx%xxx*1*9*11111111*2%23222334

What I need to do is add the three '9's separated by the '*'s together using a string tokenizer.

Any help would be appreciated thanks,

Mike
oliveriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you be a little clearer on what needs to be done here?   =)
0
oliveriAuthor Commented:
well....i dont know if I can I am a bit of a beginner at using Java :(

we I have a data file which I have read into the data file below:
                        X                              X
xxx%xxx%xx*6*9*1111111*4#xxx*4*9*2222222*3%2121212122$xxxx%xxxxxxxxx%xxx*1*9*11111111*2%23222334
          X

I need to add the three '9's (I have marked with an X) together and display the total. Is that any clearer?
0
oliveriAuthor Commented:
edit:

I have a data file which I read into the STRING below
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Éric MoreauSenior .Net ConsultantCommented:
Something like this?

Private Sub Command1_Click()
Dim strX As String
Dim strY As String

    strX = "xxx%xxx%xx*6*9*1111111*4#xxx*4*9*2222222*3%2121212122$xxxx%xxxxxxxxx%xxx*1*9*11111111*2%23222334"
    strY = "*9*"
    MsgBox (Len(strX) - Len(Replace(strX, strY, ""))) / Len(strY) * 9
End Sub
0
oliveriAuthor Commented:
well that may work but it is a bit too advanced for me, I need to be able to understand it. The only Java classes I can use are String, StringTokenizer and String Buffer. The string I gave is just an example of the layout. The %,$etc will always be in the same place but the xxx's could be any text and the numbers I need to add wil not always be 9s. The program needs to be flexible to work with any data which will take the same same format as the string mentioned.

Somebody mentioned a while next loop. Is that any help?
0
Éric MoreauSenior .Net ConsultantCommented:
Why are you talking of Java in the VB Controls topic? You better ask CS to move your question.
0
oliveriAuthor Commented:
good point, Ive just posted a question in the CS section asking them to do so, is that all I need to do?
0
imladrisCommented:
I have looked at this. Can you explain how you know where to add 9's? And what total needs to be displayed? Is something being added up? What?

Perhaps a couple of examples of the original string, the desired target string and the associated total would help.
0
oliveriAuthor Commented:
I have tried to mark the '9's with an X thouh the third one is abit out. I made all the numbers I need to add up '9's just so it would be easier to distinguish them from the rest of the string. What I need to do is add up all the numbers in that location together and display the total, in this case the numbers are 9s but they could be any positive integer. The problem for me is that I dont know how to specify that I want to add up those specific numbers...because the are a number of *s that separate integers I dont want to include in the sum.

ie
*int*INT*int*int#text*int*INT*int*int%int$text%int text%text*int*INT*int*int%int

The capital INTs are the integers that I want to add together and get a total from using the stringTokenizer class. Is that any help?
0
imladrisCommented:
I see which integers you want to add, but I'm trying to figure out how you determined that you need *those* integers. Especially when you add "in this case the numbers are 9's, but they could be any positive integer".

From your above example, I'm guessing some actual data will look like:

*1*9*33*44#this*5*9*43*22%21$more text%8 that%there*5*2*4*4%3

Is that right? Can the int's be more than one digit (as I'm hypothesizing) or not?

And from your specification you seem to be going at this positionally, that is you want to add the second integer you find in the line, and the 6th integer and the 12th integer. Is that right?
0
oliveriAuthor Commented:
Yeah it is the 2nd, 6th and 12th integer I want to add together, I need to write a program which can add any positive integers (may be more than one digit yes).

The actual data does look like the data line you have written.
0
imladrisCommented:
OK. Your original spec showed:

xxx%xxx%xx*6*9*1111111*4#xxx*4*9*2222222*3%2121212122$xxxx%xxxxxxxxx%xxx*1*9*11111111*2%23222334

I belatedly noticed that is a little different from what was shown later:

*int*INT*int*int#text*int*INT*int*int%int$text%int text%text*int*INT*int*int%int


In the first one there is a bunch of stuff before the first integer, in the second one there isn't. So, now I'm guessing that the critical bit is that the code detect whether the entries are integers/numbers or not, and add the 2nd, 6th and 12th numbers it encounters. If that's not right let me know and we'll concoct something else.

So for that assumption, the code could look something like:

int intcount,num,total;
boolean err;
String data;
String elm;

// get data

intcount=0;
total=0;
StringTokenizer st=new StringTokenizer(data,"*%$# ");
while(st.hasMoreElements())
{   elm=st.nextToken();
     err=false;
     try
     {   num=Integer.parseInt(elm);
     } catch(NumberFormatException e)
     {   err=true;
     }
     if(!err)
     {   ++intcount;
          if(intcount==2 || intcount==6 || intcount==12)total+=num;
     }
}
System.out.println("Total is: "+total);

Again, if I still don't have the right idea, or if I'm using something you're not supposed to, let me know and we'll sort something else out.
0
imladrisCommented:
Did that answer help?

If so it is now time to grade and close it.

If not, perhaps a clarifying question would help.
0
oliveriAuthor Commented:
sorry I have been away.

I can only use String, boolean and err are alien to me im afraid. I need to use a while loop to break it down, it is specifying the the digits I need which is causing me trouble
0
imladrisCommented:
We could replace the boolean with an integer. Then you would get something like:

int intcount,num,total,err;
String data;
String elm;

// get data

intcount=0;
total=0;
StringTokenizer st=new StringTokenizer(data,"*%$# ");
while(st.hasMoreElements())
{   elm=st.nextToken();
     err=0;
     try
     {   num=Integer.parseInt(elm);
     } catch(NumberFormatException e)
     {   err=1;
     }
     if(err==0)
     {   ++intcount;
          if(intcount==2 || intcount==6 || intcount==12)total+=num;
     }
}
System.out.println("Total is: "+total);

Does that help?
0
oliveriAuthor Commented:
Sorry, I just found out that it wont necesarily be the 2nd, 6th and 12th integer. I really just need to know how to break the string down and specify the peice of it that I want to use
0
imladrisCommented:
Well, and there's the rub. How *do* you know which piece to use? What are the rules?
0
oliveriAuthor Commented:
It is always the integer after the 2nd *. I also cant use the Exception class
0
imladrisCommented:
For the original string:

xxx%xxx%xx*6*9*1111111*4#xxx*4*9*2222222*3%2121212122$xxxx%xxxxxxxxx%xxx*1*9*11111111*2%23222334

the integer after the 2nd * looks like a workable rule for the first integer (9). It doesn't appear to provide an explanation for the other integers (9's).

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oliveriAuthor Commented:
if I can use the $ and # as delimiters also it is correct, either that or the 3rd token after each applicable delimiter. Still, knowing what you want to get at and how to code the program are two completely different things
0
imladrisCommented:
>Still, knowing what you want to get at and how to code the program are two completely different things

Absolutely. But knowing what you want is a prerequisite to coding.

Are you suggesting that the rule is:

Start at the beginning.
Count two asterisks.
Add in the following integer.
Go to the next # or $
Count two asterisks.
Add in the following integer.
Go to the next # or $
Count two asterisks
Add in the following integer.

If that is what you're needing, I could certainly code that up for you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.