• Post Reply Bookmark Topic Watch Topic
  • New Topic

Consolidate values in row elements of table, remove duplicates  RSS feed

 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am new to programming Java.  I have a text file with the following information:

Jane Doe         Course 1    Sun Feb 05 00:00:00 MST 2017 Present Always present.
Jane Doe         Course 1    Sun Feb 12 00:00:00 MST 2017 Present Always present.
James Cahoon Course 2    Sun Feb 12 00:00:00 MST 2017 Absent Always absent.
Mary Waters Course 2    Sun Feb 12 00:00:00 MST 2017 Present Present this time.

I would like to place this information into a table, but by date and whether a student was Present or Absent.  However, I do not want duplicate dates.  If there are duplicate dates, I would like to consolidate the values of "Present" or "Absent." I have the following code.  I have attached an image of output so far.  As you can see, there are duplicate entries.   Also, if you have any suggestions as to how I could write this better, please let me know.  Thank you kindly.

Screenshot-from-2017-02-15-13-49-27.png
[Thumbnail for Screenshot-from-2017-02-15-13-49-27.png]
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit:

I would like the table to display, i.e. for the 12th of Jan, only one entry, but 2 present and 1 absent. And for the 5th of January 1 present and 0 absent.  (see text file output in top of initial post).  Thank you.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at your code, I see that for every line you read from the file you end up by adding a row to the model. There's no grouping going on at all, even though there's some code in the middle which acts as if it's doing grouping. you should only add a row to the model if you know for sure that the unread part of the file doesn't have any other lines for that same date.

Now you might reasonably ask, how can I know that until I actually read the rest of the file? And that's a very reasonable question. The code that you posted, even though it didn't get the grouping right, was assuming that all the entries for Feb 12 were going to be grouped together in the input file. But your description of the file didn't mention that the lines would be grouped together like that, so maybe that's not the case?

Fortunately it doesn't matter. What I would recommend is to go through the whole file and build a HashMap<String, PresentAbsent> where the String is the date you extract from the file and PresentAbsent is a class which holds two integers representing the number of times present and absent on that date. Then when you're finished reading the whole file, go through the map and fill in the rows in your table model.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your comments.  I actually do have another method which includes usage of a hashmap instead.  I just need to use another value integer for days absent for "numberDaysPresentSoFar."  Pardon my ignorance, but could you shed some light on the best way to do this please?

Here is the code:

 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note:  I put the int absence in there to show that is what I would like to have for output.  Now I just need to integrate absences into the hashmap.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I need to create an object that takes an x and y value?
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I re-read your post about building a class that holds two integer values.  I will work on that.  Thank you.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created the following Class to use for my hashmap above:



Is this correct?  If so,  I edited the getDateInfo() method in previous post with the following code, but have a lot left out because I am trying to understand how to integrate the Class index.  Please advise.  Thank you.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Index is just a holder for two int's and that Index will not be used as a key to your Map but only as a value, then hashCode() and equals() are unnecessary. Comparing two Index's doesn't really make any sense.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two other things:

1. When you create an Index object the values inside it are going to be zero. You don't need to (and shouldn't) initialize them via the constructor.

2. You need methods to increment each of the values inside it.

3. (Three other things) Why is it called "Index"?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:3. (Three other things) Why is it called "Index"?

I'd suggest calling it PresentAbsent".

or Attendance
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your comments and help. 
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give me an example of this type of hashmap?  Perhaps a link to a method?  I am not a student, so this not for homework or due for any school.  Thank you.  I appreciate it.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I do consider myself a student, in learning this....
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind,, I found some good help.  Thank you anyways.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are closing your Scanner the hard way. Find out about try with resources which can obviate the need for all those close() statements and null assignments. Java7+ only.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  That's info I definitely also needed.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!