• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble sorting array of Strings from user input.  RSS feed

 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an assignment where I initially have to take as input a number. That number will be the number of strings the user is going to type. As they are typed, they are to be sorted alphabetically, then printed. It took me a while to get this far, and I am stuck on how to properly invoke the other methods in main. There has to be at least three methods: main, one to do the sorting, and one to print the new array. I'm quite sure there are mistakes in my code, but this is definitely a challenge for me. Here is the code. Keep in mind that the methods being invoked are blank on purpose.

I am not allowed to use the Arrays, Arraylist or Collection class. This is basically to test my ingenuity and to see how arrays actually work. Pseudocode or helpful tips will suffice, as I don't want answers to copy.


 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are forgetting to pass your parameters to your methods:

 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you referring to the sort and print methods being invoked in main? I left them blank on purpose because I'm having trouble figuring out what to put as parameters. I think when I parsed the String[] user_word into an int at the beginning it messed everything up for me. When I try to pass the parameters as an int (sortStrings() and printTheArray() ), it gives me an error saying: incompatible types: java.lang.String[] cannot be converted to int[]
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh. I see now.

So instead of these methods taking arrays of ints, they need to take arrays of strings.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher, you might want to change code snippet below:
to:

Please explain, why you did in that way? What are you trying to achieve?
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That aspose link takes me to something that I have no clue how to navigate. What is this?
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it that way because I thought I had to parse it into an int since I was incrementing the element number. Greenhorn status confirmed.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it reaches this code snippet



and says incompatible types: java.lang.String cannot be converted to int

I'm trying to do an insertion sort in that method.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand why. I need to rewrite that entire sort method since I changed the initial array to a String like it should have been. I'm gonna go crunch some code and if I have trouble I'll get back to you guys. Thank you for your help!
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher, at this point probably the best way to step forward towards the goal is to StopCoding (< link to read).
Grab pencil and paper, write down in simple speaking language (don't use tech terms) what has to be done, and do not concentrate on how it is going to be done.
Once you get full understanding about the problem, then you'll be able to start writing the code.

Would you like to write down the steps over here? So we could help you then.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I would like to do that. What is needed from this program is:

1) Take a number from a user to tell the program how many words they would like to type.

2) Then, have the user type those words in.

3) Make a method that will sort these words alphabetically, preferably ignoring case since it will affect the output.

4) Print the new array in the sorted order.

That's pretty much it in simple language.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christopher,
GreenHorn here as well not sure how much help I can offer, but if I understood correctly, why don't you get the int value of each word's first character(after converting it to either uppercase or lowercase; to ignore Case effect) and then sequentially compare values? so in essence it would be exactly like comparing an int array but with an extra step, which is converting first char to int.
the code is something like this


 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't compare just the first letter.

The String API offers a method to determine which sting is greater. Can you find it?
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Same' comes before 'so' and 'sake' would come first.

Relying on the length and comparing one character at a time should smell fishy. You want to not only get the work done, it should be easily understood by the next guy reading your code what it is you're doing.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Janeice for the feedback, I just realized the mess of a code I made
you are absolutely right, I shouldn't start with the assumption that the first letters will be a mismatch. so I did merge it's code with the rest.
as for the String API, I did google search and I think you meant the compareTo()? I still havent implemented it yet, but I can imagine how more clean and understandable the code will be.

not to reinvent the wheel, but I would love your feedback on the updated code, here is the logic so far:

1- for any given two words in our Array, find out the length of the shortest one in order to set how many characters to compare(at most) ex between "same" and "samer" we have 4 char.

2- compare each characters in order ex "s" &"s" then "a" &"a"...up until last character BUT break in case of a mismatch:
if (first word's character is bigger than second's){ switch places and break}
else if(opposite){ break without changing anything}

3-if all characters are same as in case of "same" and "same/r" then whichever is the longest word comes after


4-print sorted array


I did test run it a few times and so far things are working fine, sake comes before samer comes before so. here's whole code






 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why compare characters when you can use compareto to compare strings?
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know and I'm not trying to reinvent the wheel, I guess I'm just curious how compareto() finds out which of two given strings has alphabetic priority...does it use the length of the strings and compare characters one at a time?
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the preferred method that I am using is compareto(). Also, the array is not yet predetermined. It is based on user input.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I'm enjoying the positive feedback on this website. I won't be copying any code, but this is helping me see how it all comes together.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this is getting frustrating. When I first did my code with the array as an int, it printed to the console just fine. It looked like this:

Enter number of elements to be sorted: //let's say the user inputs 3

Element 1: //user types string
Element 2: //user types string
Element 3: //user types string

Now, when I run this, it prints like this:

Element 1: Element 2: //type something, press Enter
Element 3:

Here is the code. What in the world did I do wrong?

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Sheridan wrote:Here is the code. What in the world did I do wrong?

You wrote a bit too much code without full understanding what it does. Beside that, the structure of the code makes you more confused.
You cannot follow yourself where one task ends and where other starts. Wouldn't be clearer something like this (it is just an example)?

Now you can come back where you described your tasks in english, and you would see, I just translated it to simple code lines.
So I got structure, whats happening after what, method names, what each of them have to achieve.

If you would go deeper in details to each step you described in english, you would know what to write inside the methods.

Give a try. Would you like to?
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raed Tabani wrote:I know and I'm not trying to reinvent the wheel, I guess I'm just curious how compareto() finds out which of two given strings has alphabetic priority...does it use the length of the strings and compare characters one at a time?


Your JRE has open source. you can look at the implementation. Remember that the implementation can change over different JVMs and different versions of each JVM. You are guaranteed the contract (from the API), not the internal implementation.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, there seemed to be a problem when I (the hypothetical "user") would press Enter after entering the number of strings that will be sorted. So, I added another input.nextLine(); and now it works! Here is the correct code without parsing anything into an Int in the array:


After implementing that new input.nextLine(); , the output looked like:

Enter the number of elements to be sorted: (user types a number. Let's say 3)

Element 1: (type a word, press Enter)
Element 2: (type a word, press Enter)
Element 3: (type a word, press Enter)

Before adding that extra input.nextLine(); , it printed like this:

Enter the number of elements to be sorted: (Let's type 3 again)

Element 1: Element 2: (I could only type something and press Enter at Element 2....Weird)
Element 3:

Now all I have to do is figure out my sort method and I should be good. I believe I already know how I want to do the print method. It would be a for each loop, and it would look something like:



The sorting part is going to be a challenge for me since I am wanting to utilize an Insertion Sort, but I am not using integers, and the strings are based on user input. Every example I have found uses predetermined elements in an array, whether they be strings or ints. But hey, I'm making progress.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you required for some reason to sort yourself? Most times it's better to use a provided API than to write it yourself.

String has built in comparators you can use so you don't need to sort.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am required to make the algorithm myself. I cannot use any tricks in the Arrays class, such as Arrays.sort.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's a good reason.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright friends, I finally did it. I took out some pen and paper and drew out an elementary Insertion Sort using pictures. I've always been a visual learner, so looking at this while considering the mathematics beyond the sort helped tremendously. Regretfully, I disregarded the code that has been posted and tried it out on my own. After a few typographical errors (simple mistyped operators) I got it to compile and execute as intended. If anyone else comes across this problem, I think this is a concise program to implement the methods required (bypassing the Arrays class).



Thank you all for your helpful hints and patience with us "noobs". Your respectful attitudes are greatly appreciated since I've seen some nasty comments on other sites.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher, quite good, well done.

One observation. Since you got methods: sortStrings, printTheArray which are quite good and descriptive method names, as well as they are doing some tasks according to their names.
Why you decided to do the rest of the stuff in main method? (as create array, get user inputs).
"main" method suppose to start your program, thats it, the rest of the code according to the current logic of our code suppose to lay down in a methods.

You may find this structure clearer. It is easier to read it, without any commenting of the code you can understand what the program is meant to do. It is the way easier to maintain and extend program if necessary, you can better understand your program flow.

In fact, you did a great job.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you did there Liutauras. I have not learned ".run" yet, so that's why I didn't use it. However, I really like how main is nice and short and simply runs the program. I'll look into making more methods for each task. Also, I was instructed to create the array in main.

I'm still learning the headings of methods. For example, why were all of them except main private? I've only titled methods "public".
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a shortest way to run it.

Probably more understandable for you would be:

It is exactly the same as you created Scanner class instance:

Hope it clears up some things
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Sheridan wrote:Also, I was instructed to create the array in main.
No one shouldn't stop you of giving a little bit extra to your teacher than he expects.
It is something, who suppose to make your mark higher.
I'm still learning the headings of methods. For example, why were all of them except main private? I've only titled methods "public".

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this? I removed the "insert" method to make it easier for me until I work on it some more. I'll submit the old one for the assignment, but I made a new project to try this out. I honestly like this version better. It's more concise, but I don't want to use things we haven't formally learned yet.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll submit the old one for the assignment, but I made a new project to try this out. I honestly like this version better. It's more concise, but I don't want to use things we haven't formally learned yet.
Good point, fair enough.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!