• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does this loop breakdown words into single letters?  RSS feed

 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I wrote this code long time ago,and I have a hard time understanding how does this break words into letters: for(int i = 0; i < st[j].length();
Does the first loop arrange the words into arrays and the second loop then break it into single letters? Also why is st[j].length() needed, is it needed to individualize the letters from the words?



 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is for my school work, I cannot use Stringbuilder, so what you are seeing is pretty much what I can use. Though I forgot how st[j].length() in the nested loop works again so I would like an explanation on that.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abad Ashraf wrote:Problem is for my school work, I cannot use Stringbuilder, so what you are seeing is pretty much what I can use. Though I forgot how st[j].length() in the nested loop works again so I would like an explanation on that.

st :: is an array of words
st[j] :: is a single word
st[j].length() :: is the length in chars of the single word.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"words" would have been a better variable name than "st".
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for example st would hold 3 words: attack at dawn. And the first loop would repeat 3 times from 0 to 2 while the second loop will repeat the first time 0-4, then 0-1 then 0-3 correct? Also my other question is how is st able to use length() in the nested loop?

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 9 this isolates a single word form the array of words. This is no different than how your code behaved but might make it clearer.
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this line basically: words[j].length() --> just puts the words like attack.length() and then loops over the individual characters in the second loop?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Slightly better to describe it as
"attack".length()
which is legitimate Java syntax
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your help in understanding this code! Somehow I just could not get why, but now I see it again so thanks again.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the String class has two different indexOf() methods, one takes a String as an argument and the other takes a char. Seeing as you are calling charAt(), which gives you a char, the second one is a better choice for you and saves you from doing the double quote trick.
vs
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems I will have to do more studying since I forgot about indexOf taking char as well as String.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java 8 API page is your friend.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!