• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble sorting doubles in my .txt file.  RSS feed

 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to sort the doubles in my .txt file in descending order, then overwrite it over the .txt file with it sorted. I have the method to sort the Strings done and finished already, but am having trouble sorting the doubles in the .txt file.

My .txt file looks like this :

Zoidberg,100.0
Homer Simpson,50.0
Peter Griffin,45.0
Lisa Simpson,100.0
Bob Burger,78.0
Marge Simpson,87.0


My code for the sorting the doubles looks like this.



I have it set up where the user can choose either to sort the names in alphabetical order, or to sort it by grades in descending order. When I select the sort grade option, nothing happens eclipse says it's terminated and nothing happens with my .txt file. Am I doing something wrong syntactically or logically? Any help would be great thanks.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing all the commas in the input file to spaces.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try changing all the commas in the input file to spaces.


You mean like this?

Zoidberg 100.0
Homer Simpson 50.0
Peter Griffin 45.0
Lisa Simpson 100.0
Bob Burger 78.0
Marge Simpson 87.0

EDIT: That actually worked, but I'd like to keep the commas in there though. It also just appended the numbers only to my .txt How would I get it to print the names as well? Thanks.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by reading the Scanner documentation and you will see why the comma was a problem. But you will have to be quick before I go to bed.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will I have to do something like ?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have obviously identified the problem, but that won't quite work.

You started with the default delimiter which reads something like this "\\p{javawhitespace}+". YOu can confirm that by creating a Scanner instance and printing out
System.out.println(myScanner.deliimiter());
or something rather like that. Note the \\ will turn to \ because of escaping.

Since line end characters count as whitespace, if you look at a line line
Homer Simpson,45.0
… that breaks into Homer and Simpson,45.0 (terminated by the line end). Your comma will append the beginning of the next line to the number.
You can try a delimiter which is either whitespace or comma or line end or comma. Try the following regular expressions, but I am very rusty on regexes so there is a good chance I shall be mistaken about them:
"[\\s|,]"
"[\\n|,]"
If I got them right which I probably haven't (‍) they will divide that line into Homer Simpson 45.0 or
Homer Simpson 45.0.

More to follow in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have worked out why using comma alone as a delimiter will append the first half of the following line to the number. And whichever delimiter doesn't give you 45.0 with nothing else attached will prevent you recognising that as a number at all.

As for printing the names, I am afraid I am going to tear your code to pieces. For a start, you have a method which does too much. A method should do one thing and do it well. You are reading from the source, then you are sorting a List, then you are writing back. That makes three methods to me. And if they are going to share the List, maybe that List should be a field. And if that List is a field, maybe you will have different files to read and different Lists with different contents. In which case the List mustn't be static. Nor should any of your other methods be static.
Also you are not closing the Scanner. I thought at first that might be the cause of your not writing the numbers, but it isn't. You need to get into the habit of thinking
I absolutely must close everything which reads or writes from or to anywhere.

Except System.in System.out and System.err
In days gone past that meant you had to write a finally block, but in Java7 they introduced a new mechanism which allows you to write something in such a way that the JVM guarantees to close it. Simply writing myWriter.close() is not sufficient (even though you will see it in books) because there is a risk that an Exception will leave your writer open. If you close any of the three streams in the System class or a reader writer Formatter or Scanner pointing to them, you will have no end of trouble later.

I would suggest you should have a class which encapsulates name and score. You can sort a List of such classes if the class implements Comparable<Something> or if you can supply a Comparator. And I am too tired to explain that now. But I can point you to a part of the Java® Tutorials which tells you all about that sort of thing, as well as the sort of pitfalls you can encounter. In the case of marks you can use this sort of thing (I hope you understand the ?: operator)
return mark > other.mark ? 1 : mark == other.mark ? 0 : -1;
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return mark > other.mark ? 1 : mark == other.mark ? 0 : -1;
is equivalent to
return mark > other.mark ? 1 : (mark == other.mark ? 0 : -1);
because ?: associates to the right.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I appreciate your help, I'll read the tutorial you posted and try to figure it out. I'll let you go to bed. Thanks again.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Twenty minutes ago, I wrote: . . .
System.out.println(myScanner.deliimiter());
or something rather like that. . . .
Then I noticed the spelllling error.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!