• Post Reply Bookmark Topic Watch Topic
  • New Topic

not sorting string  RSS feed

 
vinayGuddu Pandey
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,
I have written one program for sorting but while running it is not displaying the correct output

in output it is displaying the zeroth element but not sorted one
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What output are you actually getting?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:What output are you actually getting?

...And what are you expecting?
 
vinayGuddu Pandey
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting this output


but I am expecting the sorted String
Actually I have written "sister vinay mummy appa "
this in my file .So it should display like appa. but it is showing sister why.....
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're reading each line of your file (line 20), and then you're breaking each line into words (line 32) and only using the first word (line 34). If you want to output every word in the line, you're going to have to add them to the list.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is this method supposed to do?


you pass in a String "sng". you split the string, and put the elements into an array "token" (this should probably be called "tokens", since there could be more than one).

You then call toString on the array, which gives you this line:
[Ljava.lang.String;@10385c1

because that is how java prints an array. If you want to print the elements of an array, that is something different.

You then add a single element from your array to your soundtrack. If your file contains a single line of "sister vinay mummy appa ", then you are only adding "sister" to your soundtrack collection.

So, when you print it before you sort, it prints the one and only element.

When you print it after your sort, it prints the one and only element.
 
vinayGuddu Pandey
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got your point Fred, but if i need shorted element then what should I write the code and toString() method are used to represent object in meaningful language.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry but I don't understand what you are asking.

perhaps it would be best if we started over. Tell us what exactly you are trying to do. don't talk using java terms, talk in English. For example:

I have a file of email addresses. The file can have multiple addresses per line, and may have multiple lines. On a given line, a new email address is separated with a semi-colon. A single email address will not be split across multiple lines.

I need to read the file, parse out each email address, and then send a stock email to each address in the list.

From the above, it is fairly clear what I need done. Further, if you read it, you have an idea, or can at least ask me questions to clarify. I haven't really used any 'java' terms. i'm not trying to think as a programmer...I'm trying to explain it to (say) an intern.

Can you do that so we know what you are trying to do?
 
vinayGuddu Pandey
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,
I have a file of name Songs.txt in that I have written some text separated by blank/space like
"I am enjoying coding, just need guidance"
So first I need to split this by blank space then sort it.
So I think after sorting "am" word come very first
that value should get display, that all I want.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, look at your addsong() method. You're splitting on white space - I think that bit's working fine. But then you go:
How do you expect that to ever add anything other than the first word?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So..the file has exactly one line. This code:


reads that ENTIRE line (and thus, the ENTIRE FILE into the string s.

You then pass this string into the addsong() method:


So, on line 1, "sng" will hold "sister vinay mummy appa "
you call split() on that, putting the results into the array named "token". So, that array holds four elements, each being a string.

You then call toString() on that array, and print it. that gives you this:
[Ljava.lang.String;@10385c1

This probably isn't what you want, but that is what you get when you call toString on an array.

So then, you add the 0th element of your array "token" to your soundtrack. That means you have ONLY added "sister" to the soundtrack ArrayList. You then exit this method.

you then get to this line:

Your soundtrack only contains one element, and that is what it prints out:
before sorting [sister]

you then sort the ArrayList. Since it only has one element, nothing changes. You then print it out again (its one element):

and get that one element printed:
soundtrack are[sister]


If you want to get ALL the elements into your ArrayList, you need to put them all in there. instead of this:

you need code that iterates through the array "token", and add EACH element to the soundtrack collection.
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps, modifying addSong(String sng) to following should solve the problem



Please let me know otherwise

- Rakesh
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!