Casey Clayton wrote:Pulling info from a string. I have a string which contains some results such as [United States of America 1 Netherlands 0, Canada 1 United States of America 1, United States of America 3 Russia 1, Norway 2 United States of America 1, Sweden 2 United States of America 4"]. Now based on user input I need to pull the numbers from this string and add them up. So if the user inputs United States of America it would pull 1,1,3,1,4 put that in an array and add it. I know how to sum the array just not sure how to get that information from a string, any ideas?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Casey Clayton wrote:Pulling info from a string. I have a string which contains some results such as [United States of America 1 Netherlands 0, Canada 1 United States of America 1, United States of America 3 Russia 1, Norway 2 United States of America 1, Sweden 2 United States of America 4"]. Now based on user input I need to pull the numbers from this string and add them up. So if the user inputs United States of America it would pull 1,1,3,1,4 put that in an array and add it. I know how to sum the array just not sure how to get that information from a string, any ideas?
Yup. These look like match results (ice-hockey, I presume ), so how about creating a MatchResult class that understands how a "text result" is formatted (you haven't explained that, BTW), and stores the relevant details internally?
Unless you do something along those lines, you'll simply be writing a procedure that could have just as easily been written in C.
One you have such a class, you can then store arrays (or Lists) of them, and write an isInvolvedIn(String) method for it. Do you see how that might help you?
It's possibly overkill, but you might find reading the FirstClasses page useful - but I warn you: it's quite long.
Winston
Tim Driven Development | Test until the fear goes away
Tim Cooke wrote:Take your string:...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Hope you don't mind.
Tim Driven Development | Test until the fear goes away
Tim Cooke wrote:The first thing to do is to forget about coding for a minute. figure out how you'd do it with a pen and paper.
Take your string:
"United States of America 1 Netherlands 0, Canada 1 United States of America 1, United States of America 3 Russia 1, Norway 2 United States of America 1, Sweden 2 United States of America 4"
and figure out how you'd work out the scores by hand. Can you see any patterns?
Have a go and let us know how you get on.
Casey Clayton wrote:Ok so I know that after each team is a space and then the score, then another space the next them then their score, then a comma.
Casey Clayton wrote:Now I know that the first team is going to be the winning team.
Casey Clayton wrote:So I would match all the teams that are the same as what the user put in then take the number that is one space after that.
Casey Clayton wrote:Now I know that the first team is going to be the winning team
Tim Driven Development | Test until the fear goes away
Tim Cooke wrote:
Casey Clayton wrote:Now I know that the first team is going to be the winning team
I'm not sure you do know that. For example you have a match as "Sweden 2 United States of America 4"
I would tackle this as a two step thing.
Step 1: Separate the results into matches. I see a pattern "<match results>,<match results>,<match results>". A list of MatchResult's delimited with a comma.
Step 2: For each MatchResult, find the score for each Team. I see a pattern "<first team name> <first team score> <second team name> <second team score>". Or more generally "<some words> < a number> <some words> < a number>".
So that's the general pattern I see for parsing the String into the components you need. The second step is to store that data into some Java objects.
However, let's not get into that just yet. Let's get the parsing functionality working first and just verify your results using some System.out.println() statements for simplicity.
Tim Driven Development | Test until the fear goes away
Casey Clayton wrote:could I possible get an example of how I would use MatchResult?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Tim Cooke wrote:Sorry, I didn't mean that MatchResult at all. I wasn't intentionally referencing any Java API. I was just talking in a language that made sense to me with respect to your problem domain. I'm going to edit that post to get rid of the auto links.
Casey Clayton wrote:Haha ok! So I was thinking something along the lines of maybe making a hashmap for each result set such as Team being the key and score being the value all stored in an arraylist so something like.
Casey Clayton wrote:I could do a for loop that loops through the array and then a nested for that loops through each hash..
Casey Clayton wrote:I am really completely clueless on how to parse a string. Never even touched on it in class.
R. Jain wrote:
Casey Clayton wrote:Haha ok! So I was thinking something along the lines of maybe making a hashmap for each result set such as Team being the key and score being the value all stored in an arraylist so something like.
Seems like a good move. But why an ArrayList? If you're storing the mapping from team to score, it should be just a Map with String as key, and Integer as value.
Casey Clayton wrote:I could do a for loop that loops through the array and then a nested for that loops through each hash..
So, you're thinking of creating an array of HashMap, with each array elements corresponding to one GameResult. Can you think of some better idea? Probably thinking towards creating your own structure, that will store a single GameResult, and have a List of that data structure?
Casey Clayton wrote:So I was thinking something along the lines of maybe making a hashmap for each result set such as Team being the key and score being the value all stored in an arraylist so something like.
[{team1: 1, Team2: 3}, {team1: 5, team2: 6}] etc. That way I have all the matches seperated,
but this causes the math to get a little complicated.
I could do a for loop that loops through the array and then a nested for that loops through each hash and pulls the value for the key where the keyname = input but no clue how to put that into code and could be over complicating it.
I am really completely clueless on how to parse a string.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Casey Clayton wrote:The logical thing to do would have been to store the matches in a hash as follows {"USA VS NORWAY" => 1, 2}
Winston Gutkowski wrote:
Casey Clayton wrote:So I was thinking something along the lines of maybe making a hashmap for each result set such as Team being the key and score being the value all stored in an arraylist so something like.
[{team1: 1, Team2: 3}, {team1: 5, team2: 6}] etc. That way I have all the matches seperated,
NOW you're getting the idea,
but this causes the math to get a little complicated.
Not quite sure why.
I could do a for loop that loops through the array and then a nested for that loops through each hash and pulls the value for the key where the keyname = input but no clue how to put that into code and could be over complicating it.
I am really completely clueless on how to parse a string.
I think you may be overthinking this. If you have a class (GameResult?) that encapsulates the result of a single game, then all you need to do is pass it a String containing the result of a single game, and have it create an instance for you.
And, yes, it does require some parsing, but this is where reading the API docs is really useful (and I advise you to get into the habit of having this page open on your desktop ALL THE TIME).
The fact is that you don't need any fancy split() methods or StringTokenizer's (although they do make life easier); you can do it with only substring() and charAt().
But it does take a bit of work. Did anyone tell you that programming was easy?
Winston
Casey Clayton wrote:So after running a for loop to print the tokens array I get this output, which is a good start along the right path.
R. Jain wrote:
Casey Clayton wrote:So after running a for loop to print the tokens array I get this output, which is a good start along the right path.
Good. So, now you've separated and printed different games. But I strongly suggest you to take a step back. Take out a paper, and pen. Write down the domain objects that would be needed for a Game, as I detailed in my previous post. That would make your job easier moving further. Don't write any code. Just jot down the objects.
Casey Clayton wrote:Ok so we are going to have a Team object, Score object, and a Game object and maybe a collection object.
Casey Clayton wrote:I'm not sure what to do with this. We have never done anything with the multiple object/class setup at all besides discussing it.
Casey Clayton wrote:Score should extend team, game should extend score and collection should extend the game object I am thinking
R. Jain wrote:
Casey Clayton wrote:Ok so we are going to have a Team object, Score object, and a Game object and maybe a collection object.
Good. All seems fine.
Casey Clayton wrote:I'm not sure what to do with this. We have never done anything with the multiple object/class setup at all besides discussing it.
Don't worry. We'll do it here only. Everyone starts somewhere.
Casey Clayton wrote:Score should extend team, game should extend score and collection should extend the game object I am thinking
Great. That is a very good grasp till now, of what we've discussed. But think a little more. What type of relation is there in between - Score and a Team? Can you say that a Score IS-A Team? But you can say that, every Score has an associated Team. Similarly, a Game is not a Score, but it consists of 2 Scores. So, should the extend keyword be there? If not, what should be modified in that statement?
Casey Clayton wrote:Well a team has a score
Casey Clayton wrote:a game has multiple scores and has a couple team.
Casey Clayton wrote:So game should have a team object initialized in it where team has multiple score objects inside it?
Casey Clayton wrote:Could we possibly skype to help me walk though it if possible? claytoncasey is my skype name
R. Jain wrote:
Casey Clayton wrote:Well a team has a score
Hmm. Some progress there. But should that relation be - team has score, or score has team. I know it sounds a bit reversed than what normally we visualize, but there's where you would see some deviation from the real world in the programming world. To understand how, can you count a Team as an independent entity? Does its existence depend upon the existence of Score? No, right? But it's the other way round. A Score can exists only in association with a Team.
Casey Clayton wrote:a game has multiple scores and has a couple team.
Ok, A game should have a couple of Scores. And for now, let's make it to have a couple of Teams too.
Casey Clayton wrote:So game should have a team object initialized in it where team has multiple score objects inside it?
Umm. Why should a team have multiple score? We're talking about a single Game right? A single Game will have 2 Teams, each with their respective scores. Right? So, a Game should really have a couple of Team and a couple of Score.
Casey Clayton wrote:Could we possibly skype to help me walk though it if possible? claytoncasey is my skype name
Sorry brother. I don't have skype installed yet. Don't worry, we can proceed here only. It will sure take some time, but it would be worth the effort. Ah! Also, I'll go for dinner. Will be back in 15-20 minutes. By then, you can keep posting your proceedings.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:Do you need to account for U.S.A. playing Netherlands more than once? If there is a regular season, or a round-robin tournament, this is a very real possibility. It may be out-of-scope for this particular project, though.
Casey Clayton wrote:I did edit and correct myself on the team having more than one score though.
Casey Clayton wrote:So a game can't exist with out a team or a score so I kind of understand why it's like that.
Casey Clayton wrote:Well a team has a score, a game has multiple scores and has a couple team. So game should have a team object initialized in it where team has multiple score objects inside it? Could we possibly skype to help me walk though it if possible? claytoncasey is my skype name
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
R. Jain wrote:
Casey Clayton wrote:I did edit and correct myself on the team having more than one score though.
Please don't edit your posts, as it makes the further reply a bit absurd.
Casey Clayton wrote:So a game can't exist with out a team or a score so I kind of understand why it's like that.
Great. So, now that you've a list of all possible objects - 4 identified till now (including collection). Now write down the relationship between the objects that you identified on the paper. Start with independent object. And write objects that will probably depend on that. so on... If you want to put it in the form of a diagram, it is called Domain Model, where each box denotes a single domain object, and arrows identify the dependencies, and relationships.
Once you're done with that, we can start with creating skeleton classes.
Casey Clayton wrote:Well it seems that Team should be the independent object in this situation due to the fact that it can exist with out any of the other objects.
Casey Clayton wrote:Scores will rely on the team object, game will rely on both team and score object
Casey Clayton wrote:and collection will rely on the game object.
R. Jain wrote:
Casey Clayton wrote:Well it seems that Team should be the independent object in this situation due to the fact that it can exist with out any of the other objects.
That's correct.
Casey Clayton wrote:Scores will rely on the team object, game will rely on both team and score object
Should a Game depend upon both Score and Team? Think of it. Since Score already depends upon Team, can we remove the Team dependency from Game, and have just a couple of Score objects in it?
Casey Clayton wrote:and collection will rely on the game object.
Yup, that's fine.
Casey Clayton wrote:Ok so how would I go about creating these skeleton classes as you called them.
R. Jain wrote:
Casey Clayton wrote:Ok so how would I go about creating these skeleton classes as you called them.
How do you create a class in Java? Create 3 separate source file, 1 each for Team, Score and GameResult. Each source file contain a single public class with the same name as the file.
Now, how do you define a dependency between 2 classes? Have you been taught about references?
Casey Clayton wrote:We really haven't been taught hardly anything, we have a HeadFirstJava book in pdf format and we get assigned random chapters from that to read but that's really all we do, go over some fine points in class get thrown into test questions like this one with no clue on how to do it.
Our pay is based on how well we do on these tests as well which kind of sucks.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Programming is thinking, not coding; and coding is (or should be) a translation of what you already know.
Tyson Lindner wrote:Tell your teacher that there are no ties in hockey.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tyson Lindner wrote:Tell your teacher that there are no ties in hockey.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Consider Paul's rocket mass heater. |