Link to home
Start Free TrialLog in
Avatar of oliveri
oliveri

asked on

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
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Can you be a little clearer on what needs to be done here?   =)
Avatar of oliveri
oliveri

ASKER

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?
Avatar of oliveri

ASKER

edit:

I have a data file which I read into the STRING below
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
Avatar of oliveri

ASKER

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?
Why are you talking of Java in the VB Controls topic? You better ask CS to move your question.
Avatar of oliveri

ASKER

good point, Ive just posted a question in the CS section asking them to do so, is that all I need to do?
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.
Avatar of oliveri

ASKER

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?
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?
Avatar of oliveri

ASKER

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.
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.
Did that answer help?

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

If not, perhaps a clarifying question would help.
Avatar of oliveri

ASKER

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
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?
Avatar of oliveri

ASKER

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
Well, and there's the rub. How *do* you know which piece to use? What are the rules?
Avatar of oliveri

ASKER

It is always the integer after the 2nd *. I also cant use the Exception class
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oliveri

ASKER

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
>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.