• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method isn't returning anything?  RSS feed

 
Ken Whitestone
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if someone could help me understand a problem I've run into while trying to get a method to return a string.

I'm working on a exercise that is supposed to return a string made of the first two, third two, fifth two, (and so on, depending on the length of the string), characters of a given string. I have a list of arguments saved and ready to be passed to my main method using the IDE.



Now, regardless of whether or not the code in the altPairs() method will produce the desired output (i.e. kittens -> kien), I need help understanding why there is no output at all, and why it seems as though I have an infinite loop as the program never terminates itself.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


String s gets longer with every iteration, s.length() increases all the time - how should this loop end?
 
Ken Whitestone
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
String s gets longer with every iteration, s.length() increases all the time - how should this loop end?

Ah, yes...I see it now.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to understand that string are immutable. Also remember that in Java object variables are references to objects not the objects themselves.
Doing a + operation on the string creates a new string. So, when you do this


The first line creates a string that stores "b" and stores the reference to the string in variable s. The next line creates a new string by appending the character "a" to "b" and now stores the reference of the new String in variable s. SO, there were 2 strings created in memory ("b" and "ba"). After the first line s stores a reference to "b". After second line s stores the refernece to "ba". s is not the string, s is the reference. If you do s.length() after each of those lines, you will see after first line, it returns 1, and after second it returns 2.

So, now, apply this information to your code to see how it results in an infinite loop in the altPairs method. Go line by line and see what happens


Let's say you passed "kitten" as input
1) Line 15, i = 0, s. length() = 6
2) Line 16 s.substring(0,2) returns "ki"
3) s + s.substring(0,2) results in "kittenki"
4) s = "kittenki"
5) back to line 15. Now i = 2, but s.length()=8
6) Line 16 s.substring(2,4) returns "tt"
7) s + s.substring(2,4) results in "kittenkitt"
8) s = "kittenkitt"
9) back to line 15. Now i = 4, but s.length()=10
and so on

At every iteration you are extending the length of s by 2 while you increment i by 2. The condition i<s.length() will always be true. So, you will never get out of the loop.

There are other problems with the code. You are really not getting alternate characters. I would suggest that you use this same method of stepping over your code line by line to find the problem>
 
Ken Whitestone
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, both, for pointing me in the right direction.



...this works a lot better. It still drops the last character from Strings that have an odd number of characters, but at least this is a step in the right direction.
 
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
Tip: You can use the % operator to check if a number is odd or even. If x % 2 == 0, then x is even, otherwise x is odd.
 
Ken Whitestone
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Tip: You can use the % operator to check if a number is odd or even. If x % 2 == 0, then x is even, otherwise x is odd.

Yep, I'm already ahead of you...thanks!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!