• Post Reply Bookmark Topic Watch Topic
  • New Topic

Troubleshooting Java Code  RSS feed

 
Patrick Archard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I've been completing a small challenge whilst starting Java. It's a letter sort, and seems to work fine for 6 letters, however when I use 7 letters it seems to malfunction (my tests respond: TRUE, TRUE, "FALSE - ichaelM". I've tried it with another word, and it still malfunctions. Below is my code, any advice on where I've gone wrong would be appreciated!


 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your post. It helps preserve the formatting, making it easier to read.  Here is a page that explains how to do it yourself for next time.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick,
Welcome to CodeRanch!

The problem isn't the length of the string. Try this:


The problem is related to the number of characters that are out of order. Basically, you are trying to sort a list in one pass of an array. That doesn't work. Try to to the Michael case on paper. How many swaps do you need? Which ones?
 
Patrick Archard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thanks, and apologies first time posting in one of these.

So the aim of the exercise and the code above is to turn the word/string into a string in alphabetical order (a-z). In order to test this initially, I've used boolean tests to see if it worked properly. The actual output of this code is:

java Example (filename)
Test 1 : true // (so Hello turned into eHllo correctly)
Test 2 : true // (so Bobbin turned into Bbboin correctly)
Test 3 : false
ichaelM // (therefore Michael didn't turn into acehilM correctly).

The test in question is on lines 14/15. I'm just unsure why it didn't work considering the other 2 tests did work.

Cheers



 
Patrick Archard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Patrick,
Welcome to CodeRanch!

The problem isn't the length of the string. Try this:


The problem is related to the number of characters that are out of order. Basically, you are trying to sort a list in one pass of an array. That doesn't work. Try to to the Michael case on paper. How many swaps do you need? Which ones?


Thank you! I'll revisit this.

Patrick
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Agreed with Jeanne. Your sort algorithm just doesn't work.... which is too bad, because if it did, it would have an amazing linear big-O time complexity.

Patrick Archard wrote:
The test in question is on lines 14/15. I'm just unsure why it didn't work considering the other 2 tests did work.


Well, that is the thing about algorithm bugs. Just because it produces a few correct results doesn't mean that the algorithm is correct. To be correct, it has to produce correct results for all of the expected input.

Henry
 
Junilu Lacar
Sheriff
Posts: 11493
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
Look carefully at your test data and results. In the first two cases, only one letter needs to be moved into place relative to all the others. That's why it works. In the case of "Michael", only the "M" was moved into place relative to the others to produce "ichaelM".  All the rest of the characters are still not in their proper relative positions. What you did was implement one part of the Bubble Sort algorithm. Now you just need to go back and implement the rest of it.

Here are other words that will fail with your current implementation because there are multiple characters that are out of place and the first character that's out of place needs to be moved to the end:
Coding --> Cdingo  // "ing" still out of order
Bubble --> Bbbleu  // "le" still out of order

Your implementation will still fail with this one but unlike in the two above, multiple characters will be moved into their correct relative position while others will not.
Algorithm --> Agloirhmt

And this will succeed because only one letter is out of place:
Sort --> orSt
 
Patrick Archard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for taking the time to offer your helpful suggestions. I'm still learning the ropes of Java so will have another go at making it work for all swaps - and appreciate that the tests work correctly, it's the algorithm that may not! Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!