This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to alternate between two sets in a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to alternate between two sets in a string?" Watch "How to alternate between two sets in a string?" New topic
Author

How to alternate between two sets in a string?

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
I'm trying to write a method that accepts a string of names followed by integers, and the names alternate between boys and girls.
For examples: "Mark 41 Jennifer 22 Daniel 49 Jessica 67 Joshua 87"
The output should be:
3 boys, 2 girls
boys sum = 177
girls sum = 89

I'm having trouble just figuring out first of all how to alternate, and then how to get past the names to the integers.
Any help is appreciated, thank you!

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Good Job Ashley, almost there...... check this code. Take another variable 'i'

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
I compile it and it runs without error, but nothing prints out and the run does not end. Is it because the string starts with a name?

This is my complete code right now:


Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
problem is with line 19(remove semicolon), line34(remove it)
Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
Oh wow, I see. Thanks for catching that! My full program works now!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to alternate between two sets in a string?
 
Similar Threads
System.out.println confusion with the quotation marks and + sign
Please critic, rate or give some helpful advice
sum up numbers
Using percent Modulus to calculate total number of even numbers from user input
int cannot be derefrenced