• Post Reply Bookmark Topic Watch Topic
  • New Topic

code clarification  RSS feed

 
Edward Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The note on the side of this little piece of code says that the string associated with "i" is appending to the outputted string while the "t" is removing from. I'm not seeing how this is happening in this code and personally think the description could be a bit more in depth. Also the expression baffles me for some reason. The book associated with this course explains it but it's not sticking. Can anyone describe what is happening in that expression in easier terms?
Screen-Shot-2013-07-06-at-6.23.17-PM.png
[Thumbnail for Screen-Shot-2013-07-06-at-6.23.17-PM.png]
 
Edward Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the meaning of that one line to be the invoking of the length method from the "i" object.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not convinced this is a particularly good example, it just makes a simple thing look very complicated. It is perfectly legal Java but just because you can do something doesn't mean you should do it.
Personally I'd write as follows (I actually wouldn't use i as one of the variable names as it is generally used for an integer index counter but I have used it here so you can easily compare this code with your example):


 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the description could be a bit more in depth



.....on the contrary I think side note is quite descriptive. It clearly states that in third part of for loop, you are appending to
one string(here it's, i += "|") and removing from other one(here it's, t = t.substring(1) ).

So in your for loop, your first part initializes i and t, second part validates that string referred by i has length less than 7 and
in third part you are making changes in i and t.
So for loop does this:

i and t are initialised(this happens only once at start of for loop)

then it is verified whether i's length is less than 7
if yes, then body of loop is executed, and then changes are made as per third part.
verification of second part is done again and so on....
till validation fails


Manish

 
Edward Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being a beginner I was unaware of the purpose of the substring method. It is now a bit more clear. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!