• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Can I Do a Compare statement within a try/catch?  RSS feed

 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code using Collections.sort to sort arraylist "lines."  I want to use the compare to sort the text file by the third element (className), but do not know if I need to write a separate class to do this, or can I just write a method within the try/catch statement?  I'm new to this.  Any help would be much appreciated.

 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, would I use compare or compareTo and use list.get(3) for instance?
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I have the following within the try/catch statement.  Is this correct?
Collections.sort(lines, new Comparator<String>() {
                @Override
                public int compare(String s1, String s2){
                    return s1.compareTo(s2);
            }
            });
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Stuart wrote:I want to use the compare to sort the text file by the third element (className), but do not know if I need to write a separate class to do this, or can I just write a method within the try/catch statement?


Please elaborate what you mean by "sort the text file by the third element (className)". Thanks.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give more details. We don't know what the lines in your input file look like and you haven't given enough information except to say there's a third element. Show us one or two lines from that input file so we know exactly what you're dealing with.

To save time and more back and forth like this, you might want to give us an idea of what the lines in your output file should look like.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file which has firstname (tab) lastname (tab) and className (tab).  I need to sort by using classname only, i.e. class2 would be before class3.  I don't want to to sort it by the first element, firstname.  I made an arraylist of the textfile and now I am trying to get it to sort properly.  I now have the following within my try/catch phrase:


I am wondering whether 0 means the first element, firstname and I am including the tabs, so I am using (4) for the className field.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input file looks like this: (with tabs)

Jane Doe    Course 2 Sun Feb 12 00:00:00 MST 2017 Present test notes
Chris Doe   Course 3 Sun Feb 12 00:00:00 MST 2017 Absent test notes
Chris Doe   Course 3 Sun Feb 19 00:00:00 MST 2017 Present present this time
Chris Doe   Course 3 Sun Feb 26 00:00:00 MST 2017 Present present again
Barry Jones Course 2 Sun Feb 26 00:00:00 MST 2017 Present present this time
Carrie Jones Course 5 Sun Feb 26 00:00:00 MST 2017 Present present this time
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want output to have sorted by classname only., as in:  But would be nice if Jane Doe "Doe" came before "Jones" as well.


Jane Doe         Course 2 Sun Feb 12 00:00:00 MST 2017 Present test notes
Barry Jones Course 2 Sun Feb 26 00:00:00 MST 2017 Present present this time
Chris Doe         Course 3 Sun Feb 12 00:00:00 MST 2017 Absent test notes
Chris Doe         Course 3 Sun Feb 19 00:00:00 MST 2017 Present present this time
Chris Doe         Course 3 Sun Feb 26 00:00:00 MST 2017 Present present again
Carrie Jones Course 5 Sun Feb 26 00:00:00 MST 2017 Present present this time
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit, I actually do want it to sort by classname primarily, then by last name. 
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh....lightbulb went off....it is sorting by line, not by column like I want to....what would I use for column sort?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Stuart wrote:Oh....lightbulb went off....it is sorting by line, not by column like I want to....what would I use for column sort?


Bingo !!  ... and of course, calling the get() method on lines variables isn't the way to go, as that is just getting a different line.

What you need to do is parse the two lines, that is passed into the comparator -- either via substring() or split(), or whatever is your favorite mechanism -- and report whether the first is less than, equal to, or greater than the second.

Henry
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks I will try this.
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but it isn't just the two lines, it is all the lines in the file.  Would I want to do a loop and capture all the lines?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be helpful if you read some articles about the benefits of functional decomposition and separation of concerns. You have neither when you write methods that are as big and as busy with multiple things as what you have now. It's like if you were made with your heart, lungs, and brain functions all mashed together in one organ. That would be bad for you and the same kind of thing is bad for your program. In other words, finding a lost needle is easier if you have a bunch of small piles of hay instead of one big honking haystack.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Stuart wrote:Ok, but it isn't just the two lines, it is all the lines in the file.  Would I want to do a loop and capture all the lines?


No. The comparator is responsible for two elements (lines) only. And it actually doesn't know which two elements it is.

The sorting algorithm decides which elements to compare (while sorting), and may call the comparator as often as needed to establish the order.

Henry
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I tried this, but it is not sorting by the third field, although it is printing out the third field, but for only the first two lines.  I am misunderstanding a bit....how can I get it to compare all the lines?

 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took out the first two lines as they seemed redundant, but it is still not sorting correctly.  Any help would be much appreciated, as I am very new to this.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Stuart wrote:I took out the first two lines as they seemed redundant, but it is still not sorting correctly.  Any help would be much appreciated, as I am very new to this.

Again, don't make statements like "it's still not sorting correctly" -- that implies that other people can read your mind and know what "sorting correctly" means to you. That, or you've assumed people have read through the whole thread and picked out that information somehow. That would be fine if the information was easy to find but when a thread starts getting long, people tend to just read the most recent responses.

Give the details -- say something like "it's sorting like it's only checking (some value) instead of sorting by (some value) then by (another value) as a secondary sort order." By helping others help you like this, you increase the likelihood of getting a resolution to your problem faster.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Stuart wrote:I took out the first two lines as they seemed redundant, but it is still not sorting correctly.


The first two lines are *not* redundant. The first two lines are completely wrong. The purpose of the comparator is to compare the two elements that are passed in by the sorting algorithm. It is not for the comparison of the first two elements only.

As for your follow up, I agree with Junilu. You need to TellTheDetails.

Henry
 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand now regarding the purpose of the comparator.  I have written the following code, using comparator to sort my text file, "pupilInfo.txt"  by the third field.  It sorts by the third field correctly now.

 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, looks like you've got the idea of the Comparator right now.

I went through your code and attached some comments, some related to things which you should be doing but aren't, and others related to relatively new features of Java which simplify the code which you have to write. Here it is:


 
Johnny Stuart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your comments and help!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!