• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pulling information from a string

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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




Well this is for a test in my Java class, we are encouraged to ask questions so I am here asking. Here is the exact question we were given. I know how to do the logic I'm just not sure how to get the need information from the string, as part of the question is to do so but we haven't been taught this yet, we are suppose to research and figure it out for ourselves.

1. Hockey Game Problem

Set up a string variable to hold the following men’s hockey game results. The string should be the following (the following are made up scores ):

String results = "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";

Let’s look at the first part of this string, “" United States of America 1 Netherlands 0”.
This means that USA won the match and USA scored one goals while the Netherlands scored 0.

A win is worth 3 points, a draw 1 point, and a loss no points.


Write a program that will ask a user what team the user wishes to see statics for (United States of America, Netherlands, Russia, Norway, or Canada).

Have the program then display how many wins the this team the user specified had, how many games they had a draw, how many they lost, how many goals scored and how many goals conceded, and how many points in total that team acquired.


Have your program display how many points in total the USA has acquired.
Your program should display something similar to the following:



The United States of America:
Number of wins = 3
Number of draws = 1
Number of defeats = 1
Goals scored = 10
Goals conceded = 6
Number of points = 10


 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Take your string:...


Tim,

I removed the code block from your post because it makes the line far too long. Hope you don't mind.

Otherwise, I couldn't agree more with your advice.

Winston
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Hope you don't mind.


Of course not.
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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. Now I know that the first team is going to be the winning team. 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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Remember that there is also some spaces in the team name. You need to consider that too.

Casey Clayton wrote:Now I know that the first team is going to be the winning team.


Why? For example, see the last pair. The winning team is second. Anyways, you should never make such assumptions about the input you get. Safeguard yourself, by doing that bit of work.

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.


Would it be better to work with that string only, or to make some data structure out of those string. Do you see here some mapping from Team Name to their Score? How would you use that? Also, what are you going to do with the commas? They are there for some reasons.
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check if you can use split , a method in String Class.



 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Match Result's delimited with a comma.

Step 2: For each Match Result, 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.
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



could I possible get an example of how I would use MatchResult? Never used it so the documentation isn't really helping me out much.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Casey Clayton wrote:could I possible get an example of how I would use MatchResult?


My fault. I shouldn't have used MatchResult as a name, because it is already a class in the Java SDK (java.util.regex.MatchResult), so forget about it. How about GameResult instead?

And the whole idea is that you create it. So how you use it is entirely up to you.

Winston
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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.

[{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. Never even touched on it in class.
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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? Why I'm saying this is because of this - [{team1: 1, Team2: 3}, {team1: 5, team2: 6}]. Assuming you aren't aware of what this problem is all about. Can you figure out what that array of maps is all about? If not, then perhaps it's not the best way.

Casey Clayton wrote:I am really completely clueless on how to parse a string. Never even touched on it in class.


Well, we can see it later on. It will come way afterwards. First you should concentrate on getting right structure for your code set up. Think from design perspective first. Parsing a String, and getting information counts as business logic. That you do at the end.
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Well the whole problem in general is just confusing me I guess. The logical thing to do would have been to store the matches in a hash as follows {"USA VS NORWAY" => 1, 2} not sure the formatting on that since we have never used hashmaps in class either but saying each match team 1 vs team2 is stored as the key and the scores are stored as the value, this would have made the whole problem much simpler, rather than storing it in a string like this.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 its constructor 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
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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}


Ok, forget about hashes for a moment. Think about a Hockey game. What all things you can think of that are related to the game?
  • First of all, for a game there has to be Team
  • Then there are Score for both the teams participating
  • How does a Game comes to a conclusion? Based on the Scores of both the Teams.
  • Then here you're having a collection of different Games.


  • Can you spot some possible domain objects there?
     
    Casey Clayton
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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



    Lol no no one told me it was easy but when you are taking a training course provided by a job that hires people with no programming exp as Java Developers you would expect to be taught some of this stuff in class before being tested on it. Our last test was over taking input and doing some simple math on the input, this was a week ago, having no clue what was on this test we brushed over HashMaps, enums, etc but didn't do any practical coding with them so it's a big jump in one week.

    So I have this kinda setup but not sure where to go from here. We haven't really went over doing multiple classes so not sure how to go about it that way.



    So after running a for loop to print the tokens array I get this output, which is a good start along the right path.

    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

     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.



    Ok so we are going to have a Team object, Score object, and a Game object and maybe a collection object. 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. Score should extend team, game should extend score and collection should extend the game object I am thinking, just based on what we talked about in class briefly.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?



    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 a score object in it? Could we possibly skype to help me walk though it if possible? claytoncasey is my skype name
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.

     
    Casey Clayton
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.



    Ok I am going to lunch in about a half hour for an hour or so as well, this will give me some time to walk away and come back. I did edit and correct myself on the team having more than one score though. So a score should have a team object inside it rather than the other way around. So a game can't exist with out a team or a score so I kind of understand why it's like that.
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.



    No if you go back a few posts and look I actually put the exact question. My whole goal is to take the string they gave me and get all the need information from it based on what the user inputs.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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:
  • Game
  • Team
  • Score
  • Collection of Game. (Re-think if we really need a separate domain object for this? Or we can just do with an array of games, or a List of Game. Java API provides various Collections that you can use)


  • 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.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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


    I don't think Skype is necessary. Let's just deal with the result of a single game:And there you have it. A class (and therefore an object) that holds all the information about a single game we need.
    Yes, getting the information from a result string is kind of fiddly, and could probably be improved on, but it's certainly not rocket science.

    More importantly, now you have a class, so you can add all sorts of goodies to it to help you with all the other things you need.

    However, this is only the start. You might also want to think about a Team class, and all the stats you might want to keep for them.

    HIH

    Winston
     
    Casey Clayton
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.



    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. Scores will rely on the team object, game will rely on both team and score object, and collection will rely on the game object.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.

    Ok, you've got a class from Winston. You can get an idea of how it will look like. You can of course further improve upon the string parsing logic.
     
    Casey Clayton
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.



    Ok I was kind of wondering on that but it does make since. Ok so how would I go about creating these skeleton classes as you called them.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?



    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.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No issues. Just create those 3 classes, in 3 different source files. Since Score should have a Team associated with it, have a reference to Team in Score class (If you don't understand what reference is, just do whatever you can understand from that. We'll resolve that). Similarly, a GameResult is associated with 2 scores, so you should have 2 Score references in the GameResult class - 1 for homeTeam and 1 for awayTeam. Do these steps, and post the classes that you come up with here. Don't forget to add appropriate constructor where-ever you think is required.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.


    OK, so it sounds like you're pretty much on your own. That's bad, but it's not the end of the world if you're reasonably intelligent bloke (and it sounds like you are).

    BUT...(and I'm going to sound like a broken record here)...you CANNOT solve a problem with Java code if you don't understand WHAT is required.

    So: before you write your first line of Java code on any problem you're given, sit down and write out how you're going to solve it in English (or your native language). I also suggest that you do it with your computer TURNED OFF. That way, you won't be tempted to find out what Java class methods can help you with (and believe me, there's plenty).

    Programming is thinking, not coding; and coding is (or should be) a translation of what you already know.

    Our pay is based on how well we do on these tests as well which kind of sucks.


    Agreed. So learn to think before you code.

    Winston
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:Programming is thinking, not coding; and coding is (or should be) a translation of what you already know.


    I second that great thought.
     
    Ranch Hand
    Posts: 211
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Tell your teacher that there are no ties in hockey.
     
    fred rosenberger
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tyson Lindner wrote:Tell your teacher that there are no ties in hockey.


    Perhaps not now, but there used to be. If this is to be used for historical data, a tie is perfectly valid.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tyson Lindner wrote:Tell your teacher that there are no ties in hockey.


    ??? Only after this 'orrible, "manufactured result" nonsense. Have you seen the NHL league tables these days?
    If you understand 'em, you're a better man than I am.

    AFAIK, the the NHL is the only league that uses it; and even then games can still end in a draw - at the expense (possibly) of a few seasons off a player's career because of extra time.

    IMO, It says more about the American public's inability to understand a game that can possibly end in a draw than anything else.

    English football got it right: 3 points for a win, 1 for a draw. End of story.

    Winston
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic