Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading, Comparing and Updating Text files  RSS feed

 
Steve Haggan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I hope someone can give me some valuable pointers.
I am new to Java and still struggling to get my head around some/most concepts
I have been provided with two text files, one is a league table of football teams and their current points which I have stored into two separate arrays - teams and points
The second text file is a list of teams and their last results, however the results are in text form thus I have had to amend to include numeric values i.e 3, 1 or 0.
The task is to read the second text file and update the two arrays as appropriate
My thoughts are that I scan the text file firstly through the first array, to find out the teams current position in the league, when that position has been established I use that position to scan through the second array to find the points total at that element of the array and then update that points total with the numeric value in the string.
My problem is I dont know how to go about reading the string in the text file into the first array.
My current code is below, (im not sure if this is the correct way to upload it to the forum) if anyone has any pointers I would be most grateful, and apologies if I have not made my thoughts clear


 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:Hi, I hope someone can give me some valuable pointers.

Hi Steve, and welcome to JavaRanch.

When you're posting code, please UseCodeTags (←click). And please read the page thoroughly, because there are a few 'gotchas' to know about.

I've done it for you this time, and you can probably see that it looks a lot better; however, I don't fancy correcting all the indentation, so I suggest you do that yourself. You can use the 'Edit' button to update your post (but once you have it looking good, please don't change the content; otherwise you will just confuse people). You can also use the 'Preview' button to see what your changes will look like before you post them.

Thanks.

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:I have been provided with two text files, one is a league table of football teams and their current points which I have stored into two separate arrays - teams and points
The second text file is a list of teams and their last results, however the results are in text form thus I have had to amend to include numeric values i.e 3, 1 or 0.

It would help a lot if you could show us an example of exactly what these two files look like, along with some description.

However, just off the top of my head, I would say that you want to convert the data they contain (especially things like numbers) to the correct type as soon as you possibly can. Strings make bad substitutes for other types.
Have a look at the Integer (java.lang.Integer (←click)) class documentation, and you'll find what you need to convert integer strings to numbers.

I don't know how far you are along the Java road, but I'd also say that you want to create classes that reflect what you're trying to keep track of; which, just from your description, might well include things like Team, Match (or Result), and Table. Have a think what classes with those names might look like.

Winston
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using an input file of this:
Barcelona 34
Real Madrid 32
Villareal 27
Espanyol 25


Your code correctly loaded it into the arrays for me. (although I think it should be temp+= " " + st.nextToken(); ) Are you getting an error or incorrect results while trying to load the first file?

If you can, you could use different data structures (e.g. ArrayList) than fixed size arrays since you don't have to check for trying to add more than the array can hold. This is also a perfect problem for a Java data structure that implements the Map interface, where you have key (team name) and a value (points).
 
Steve Haggan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston, I'll make sure the next time all code is input correctly
I'm taking my first steps on the java road so I'm not a great way down it and havent dealt with classes yet.

The data in the two data files looks like:
Table 1:
Barcelona 34
Real Madrid 32
Villareal 27
Espanyol 25
.....
there are twenty teams and their points tally

The second table is their results last match:

Villareal drew
Espanyol drew
Real Madrid won
Barcelona lost
.....
So for each of the teams there is either won, lost or drew

I then need to convert won to 3 point, lost to 0 and drew to 1 point.

Then I need to go through the arrays created for the first set of data, for example find Villareal and find what position that team name appears in the first array.
When I get that element in the array, I need to go to the corresponding element in the points array and update that element by the number of points (which for Villareal in this case will be 1).

Does that make sense?
I believe thats what I need to do my Java knowledge isnt strong enough yet to convert this into English.

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Red Smith wrote:although you would need to use the other StringTokenizer constructor:

@Steve - That reminds me: Have you been specifically asked to use StringTokenizer? Because it's a legacy class, and its use is generally discouraged. The Scanner class (java.util.Scanner) can do pretty much everything that ST does, and you can use it on Strings or Streams.

Winston
 
Steve Haggan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Red Smith wrote:although you would need to use the other StringTokenizer constructor:

@Steve - That reminds me: Have you been specifically asked to use StringTokenizer? Because it's a legacy class, and its use is generally discouraged. The Scanner class (java.util.Scanner) can do pretty much everything that ST does, and you can use it on Strings or Streams.

Winston

Yes we were asked to use String Tokenizer, I know some of my more knowledgeable class mates queried this as it is an older class.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:Thanks Winston, I'll make sure the next time all code is input correctly

OK. Cheers.

there are twenty teams and their points tally

Tally with what? Seems to me that without knowing how many matches they've played, you can't tell whether they tally or not - or have you been told to assume that they tally?

The second table is their results last match:
Villareal drew
.....
So for each of the teams there is either won, lost or drew
I then need to convert won to 3 point, lost to 0 and drew to 1 point.

OK, so that sounds like something you could turn into a function (except in Java they're called methods), eg:then you could update your table with the result of this method, viz:
The above is just a very small example of what you should be doing as much as possible - break the problem down into small, manageable steps.
And I hate to say it, but in order to do that properly, you need to stop coding, get out a pencil and paper, and write down what those steps are, in English (or your native language).

You will never code your way out of a jam.

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:Yes we were asked to use String Tokenizer, I know some of my more knowledgeable class mates queried this as it is an older class.

Ugh. If you feel up to it, you might want to direct your tutor to the StringTokenizer docs, because right in the top section it says:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.

However, if you've been told to use it; you should.

Winston
 
Steve Haggan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Haggan wrote:Thanks Winston, I'll make sure the next time all code is input correctly

OK. Cheers.

there are twenty teams and their points tally

Tally with what? Seems to me that without knowing how many matches they've played, you can't tell whether they tally or not - or have you been told to assume that they tally?

The second table is their results last match:
Villareal drew
.....
So for each of the teams there is either won, lost or drew
I then need to convert won to 3 point, lost to 0 and drew to 1 point.

OK, so that sounds like something you could turn into a function (except in Java they're called methods), eg:then you could update your table with the result of this method, viz:
The above is just a very small example of what you should be doing as much as possible - break the problem down into small, manageable steps.
And I hate to say it, but in order to do that properly, you need to stop coding, get out a pencil and paper, and write down what those steps are, in English (or your native language).

You will never code your way out of a jam.

Winston

Perhaps Tally is the wrong term.
I have two arrays, the team names and their current points total.
In the new text file I have the next set of results, so for example it says Real Madrid won, a win is worth 3 points and therefore I need to scan the first array, find Real Madrid and then find the corresponding element for Real Madrdid in the points array. When I have established what element this relates to in the points array I then need to update that total in the array element by 3, so in this case it should rise from 32 to 35.
I need to do this for each result in the text file and update that teams corresponding points element based on how many points their latest result is worth.
It makes much more sense in my head, and apologies again if its not clear what I need.

I'll get my pen and paper out.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:It makes much more sense in my head, and apologies again if its not clear what I need.
I'll get my pen and paper out.

Good idea; especially if it makes sense in your head. Write out in English step-by-step what you need to do and then translate it to code. You may surprise yourself.

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:...therefore I need to scan the first array, find Real Madrid and then find the corresponding element for Real Madrdid in the points array.

Which I presume will have the same index.
Again, this sounds to me like a step (or a "task"), so why not write a method that takes a team name and scans your teams array looking for it. If it finds it, it returns the index where it was found; otherwise -1 (which can't possibly be a valid index).

Do you see how that might help?

Winston
 
Steve Haggan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Haggan wrote:...therefore I need to scan the first array, find Real Madrid and then find the corresponding element for Real Madrdid in the points array.

Which I presume will have the same index.
Again, this sounds to me like a step (or a "task"), so why not write a method that takes a team name and scans your teams array looking for it. If it finds it, it returns the index where it was found; otherwise -1 (which can't possibly be a valid index).

Do you see how that might help?

Winston

Thats what I was thinking but where I think I have a knowledge gap is telling java what to search for.
I am using the tokenizer, so Real Madrid has two tokens, Barcelona will be one token, how do I tell the method what to scan for?
i.e. first scan searches for Real Madrid and returns its element in the array, then scans for Barcelona and scans for that element in the array.

Thanks for your help and patience by the way.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:I am using the tokenizer, so Real Madrid has two tokens, Barcelona will be one token, how do I tell the method what to scan for?

Wrong question: you should be asking yourself: How do I make "Real Madrid" ONE token? ie, How to I make sure that each String in my teams array contains the name, the whole name, and nothing but the name?

Winston
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Haggan wrote:
Winston Gutkowski wrote:
Steve Haggan wrote:...therefore I need to scan the first array, find Real Madrid and then find the corresponding element for Real Madrdid in the points array.

Which I presume will have the same index.
Again, this sounds to me like a step (or a "task"), so why not write a method that takes a team name and scans your teams array looking for it. If it finds it, it returns the index where it was found; otherwise -1 (which can't possibly be a valid index).

Do you see how that might help?

Winston

Thats what I was thinking but where I think I have a knowledge gap is telling java what to search for.
I am using the tokenizer, so Real Madrid has two tokens, Barcelona will be one token, how do I tell the method what to scan for?
i.e. first scan searches for Real Madrid and returns its element in the array, then scans for Barcelona and scans for that element in the array.

Thanks for your help and patience by the way.


Your logic in the read of the first file turns Real Madrid into one token. The same logic should work for the second file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!