• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on what kind of sort to use  RSS feed

 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Datafile, in that file is a first name, last name, gender, and then 5 grades..  Example looks like this:
Mike Knight :M;55,77,78,54,44.
The data file is being read in my public class. I grab the numbers by a few lines



Now i have other classes where some of these variables are declared ( score, grades) .. I split the numbers



My output would look something like this :
Mike Knight M
grades[0]:100
grades[1]:33
grades[2]:90
grades[3]:80
grades[4]:70

What I am trying to do is now sort them in order, get their average. But the code I used is not working.

if i use these lines


it returns zeros, and then goes out of bounds for grades[4] which i think i understand because of index+1 ( i have no idea why they are zeros now.
grades[0]:0
grades[1]:0
grades[2]:0
grades[3]:0

if I try to use the arrays sort  I get
The method sort(int[]) in the type Arrays is not applicable for the
arguments (int).

Did i give enough information for someone to give some input? or is there something more specific you need?

I am used to working with int arrays, but never have i worked with a file string, took numbers from it.. and I am not sure if I still have a string, or if its considered an int array now.. Very confused at the moment.
Thanks.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

How about, just post a code and let us see first, so we could give you questions if something unclear.

The one you used and which produced unexpected results. Please remove commented out code - if possible.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't implement your own sorting algorithm - use what's already available in the JDK. (Unless you have a special reason to implement your own sorting algorithm, for example because this is a homework exercise in which you're supposed to do just that).

You mentioned that you tried to use Arrays.sort(). That method expects you to pass the entire array, not just a single element of the array. So the call should have looked like this:

And not like this, where you are passing the element at a specific index:

Also, you should ofcourse call this after the for-loop that reads all the numbers from the file, not inside the for-loop.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Are you reading from a text file? You can use a Scanner instance, but you would have to change the delimiter. Start with : then you get a String with the name in, then ; and you get the gender then revert the delimiter to its default value with the reset() method. The next token will be the marks. The first and third parts can be divided into an array with String#split(",\\s*") or String#split("\\s+"). The latter part means any number (> 0) of whitespace characters. The star means any number ≥ 0. Fortunately comma, colon and semicolon don't appear to be metacharacters according to this tutorial.
Now you have got an array for name, a one‑letter String for gender, and a String[] for marks.
* * * * * * * * *
I cannot think of a way of getting the newer Java8 classes to read directly from the file, but if you have a String[] you can turn it into ints quite easily:-Most arrays can be turned into a Stream with the method in the Arrays class. The Stream has a method to turn it into an IntStream; Its parameter is a ToIntFunction<String>, which you can simplify to use the Integer::parseInt method. You can read about using the method name alone in the Java™ Tutorials. Once you have an IntStream, you can do all sorts of things. You can calculate the average with one of its methods (note its return type: call orElse(0.0) on it). You can call sorted() followed by toArray() instead of average (...sorted().toArray()). Actually you would call average().orElse(0.0) and get the result as a double.
Note the slightly unfamiliar means of indenting: all the . operators line up vertically.
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

Are you reading from a text file?


Its a data file  , its file type says File.  if you were to open it , it would have all this information in it..  What I have done so far is break the first and last name into two strings, made them lower case, then capitolized the first letter  of names. Now I want to sort the grades, and get their average.


MARY SMITH:F;100,33,90,80,70

BILL WILLIAMS:M;90,98,99,89,88

RALPH SUMMERS:M;22,33,54,56,78

HOLLY MASTERS:F;70,72,76,80,100

My entire code are two Classes



Then my second class


The Arrays sort that was told to me above doesnt return any numbers, just the peoples names.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where to begin?


 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Dagenhart wrote:... import java.util.Vector;
Nobody uses Vector any more. Declare your Lists as type List and implement them as ArrayList (both in the java.util package).
. . . public class Second_314_Project
Have you been told to use that class name? It is a dreadful name for a class.
// ... private RandomAccessFile raf;
A Random Access File is not a text file and you cannot use a Scanner object to read it. You can probably however read an int directly from it. Read the Java™ Tutorials for details. RandomAccessFile has a method for reading an int directly.

What did you use to open that data file? If you opened it with a text editor, it is probably not a random access file but a text file and you shou‍ld consider a different way to read from it.
// ... private float[] averages = new float[10];
Don't use floats for arithmetic. I would say not to use floats for anything, but there are a few occasions when you are forced to use a float. This isn't one of them; use doubles.
. . . The Arrays sort that was told to me above doesnt return any numbers, just the peoples names.
You can sort any primitive array with Arrays#sort. Also an array of any object which implements Comparable, including String[]s, so I am not sure I understand the last problem.
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  Nobody uses Vector any more. Declare your Lists as type List and implement them as ArrayList (both in the java.util package).


I been hearing that a lot . I did some research on it, and it does seem obsolete. I did read about synchronization for Vectors while arraylist does not.. I also understand that one thread call method on a vector at a time, where an arraylist doesnt have this issue.  I guess i better get updated information instead of out dated stuff. 

Have you been told to use that class name? It is a dreadful name for a class.

I can take the underscores out of it.  I got Clean Code book, still need to read it. I have been learning Java from a 70+ year old man.

What did you use to open that data file? If you opened it with a text editor, it is probably not a random access file but a text file and you shou‍ld consider a different way to read from it.

.  WHen you click right click on file and see the properties , it says Type of file: file .  its not saved as a text file.. You can open it with note pad and the contents look like this
MARY SMITH:F;100,33,90,80,70 BILL WILLIAMS:M;90,98,99,89,88 RALPH SUMMERS:M;22,33,54,56,78 HOLLY MASTERS:F;70,72,76,80,100 "JAMES WATERMELLON:M;45,54,67,72,71 "ANTHONY ROBERTSON:M;80,80,87,78,90  ALICIA SOMERSET:F;24,67,67,55,90 MARIA TANZELLI:F;55,77,78,54,44 ROBERTA KINK:F;32,66,90,4,12 MALCOLM MOLE:M;55,76,90,90,87

All of that is on a single line.
I access with this bit of code using Eclipse or BlueJ
raf = new RandomAccessFile(fileToRead,"r");
                do{
                temp=raf.readUTF();
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Dagenhart wrote:You can open it with note pad and the contents look like this
MARY SMITH:F;100,33,90,80,70 BILL WILLIAMS:M;90,98,99,89,88 RALPH SUMMERS:M;22,33,54,56,78 HOLLY MASTERS:F;70,72,76,80,100 "JAMES WATERMELLON:M;45,54,67,72,71 "ANTHONY ROBERTSON:M;80,80,87,78,90  ALICIA SOMERSET:F;24,67,67,55,90 MARIA TANZELLI:F;55,77,78,54,44 ROBERTA KINK:F;32,66,90,4,12 MALCOLM MOLE:M;55,76,90,90,87

Anyone: could this be due to "\n" vs  "\r\n" in the file? This just doesn't seem right to me.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably not a good idea to have fixName, capitalize, fix something else in Person class. Person class supposed to receive only valid values. If you need to fix them, fix them before you pass to Person.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Daniel Dagenhart wrote:You can open it with note pad and the contents look like this
MARY SMITH:F;100,33,90,80,70 BILL WILLIAMS:M;90,98,99,89,88 RALPH SUMMERS:M;22,33,54,56,78 HOLLY MASTERS:F;70,72,76,80,100 "JAMES WATERMELLON:M;45,54,67,72,71 "ANTHONY ROBERTSON:M;80,80,87,78,90  ALICIA SOMERSET:F;24,67,67,55,90 MARIA TANZELLI:F;55,77,78,54,44 ROBERTA KINK:F;32,66,90,4,12 MALCOLM MOLE:M;55,76,90,90,87

Anyone: could this be due to "\n" vs  "\r\n" in the file? This just doesn't seem right to me.

Yep, that's the problem. File uses UNIX style end of line character "\n" instead of the Windows style "\r\n". This is why it looks wrong in notepad. You will still be able to read it with text file readers in Java.
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the help. I got it all fixed.  Like I said if I knew any better I would had done it much different and more up to date stuff. I did the best with what I had given to me from a very old Java programmer.  I have it working the way it should now.  The names are taken and broken into two separate pieces, then they  are lowercased, then the first letter is made uppercase and the two names put back together The grades are in a sorted order, and then the grades are averaged, then the names with the averages are sorted from highest average to lowest average. 
Thats it. I have it all done.

Its a mess, but I will make sure to get Clean Code read up, and make sure the work in the future is more appropriate.
Thank you.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Dagenhart wrote:. . . I did read about synchronization for Vectors while arraylist does not.. I also understand that one thread call method on a vector at a time, where an arraylist doesnt have this issue.
. . .
Synchronisation is irrelevant to your current problem because you are only using the List on a single thread.
Have you been told to use that class name? It is a dreadful name for a class.

I can take the underscores out of it. . . .
That will not improve the class name. The whole name is wrong. Give the class a name that gives an idea what the class is about.
. . . its not saved as a text file.. You can open it with note pad and the contents look like this
▭MARY SMITH:F;100,33,90,80,70 ▭BILL WILLIAMS:M;90,98,99,89,88 ▭RALPH SUMMERS:M;22,33,54,56,78 ▭HOLLY MASTERS:F;70,72,76,80,100 "JAMES WATERMELLON:M;45,54,67,72,71 "ANTHONY ROBERTSON:M;80,80,87,78,90  ALICIA SOMERSET:F;24,67,67,55,90 ▭MARIA TANZELLI:F;55,77,78,54,44 ▭ROBERTA KINK:F;32,66,90,4,12 ▭MALCOLM MOLE:M;55,76,90,90,87

All of that is on a single line. . . .
That is a text file. Aas Carey has suggested, the rectangles ▭ are displayed because NotePad cannot cope with line ends other than \r\n. I have seen that before. In which case you shoul&d not use a RandomAccessFile to open the file. RandomAccessFile is designed for binary files not text files. Use a FileReader or a Scanner. Look at this part of the Java™ Tutorials.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, the link you posted is broken.
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Campbell, the link you posted is broken.


On Campbell's behalf ;)

Lesson: Basic I/O
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for helping sort out my mistake. I have corrected the original post, too.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!