Solved

Need to count integers contained in a string

Posted on 2003-11-12
21
363 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:oliveri
  • 10
  • 8
  • 2
  • +1
21 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9736059
Can you be a little clearer on what needs to be done here?   =)
0
 

Author Comment

by:oliveri
ID: 9736092
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
 

Author Comment

by:oliveri
ID: 9736099
edit:

I have a data file which I read into the STRING below
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 9736613
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
 

Author Comment

by:oliveri
ID: 9738693
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 9739013
Why are you talking of Java in the VB Controls topic? You better ask CS to move your question.
0
 

Author Comment

by:oliveri
ID: 9739239
good point, Ive just posted a question in the CS section asking them to do so, is that all I need to do?
0
 
LVL 16

Expert Comment

by:imladris
ID: 9740675
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
 

Author Comment

by:oliveri
ID: 9740979
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
 
LVL 16

Expert Comment

by:imladris
ID: 9743650
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:oliveri
ID: 9758732
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
 
LVL 16

Expert Comment

by:imladris
ID: 9764553
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
 
LVL 16

Expert Comment

by:imladris
ID: 9811548
Did that answer help?

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

If not, perhaps a clarifying question would help.
0
 

Author Comment

by:oliveri
ID: 9827424
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
 
LVL 16

Expert Comment

by:imladris
ID: 9832454
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
 

Author Comment

by:oliveri
ID: 9867148
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
 
LVL 16

Expert Comment

by:imladris
ID: 9867398
Well, and there's the rub. How *do* you know which piece to use? What are the rules?
0
 

Author Comment

by:oliveri
ID: 9898939
It is always the integer after the 2nd *. I also cant use the Exception class
0
 
LVL 16

Accepted Solution

by:
imladris earned 255 total points
ID: 9900268
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
 

Author Comment

by:oliveri
ID: 9900692
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
 
LVL 16

Expert Comment

by:imladris
ID: 9905081
>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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now