• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is my simple code not working guys?  RSS feed

 
S.R. Clark
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
S.R. Clark
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can ignore line 5
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think that it doesn't work?

It would help if you explained your problem. What are the symptoms? Do you get a stack trace? If so, what's in it? Or does the output not match with what you expected? If so, how does it differ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37490
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Look carefully at your loop indexes. The first time, i is 4 so it looks at the character at index 3. This is good. What happens the last time through the loop?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, that loop is never entered.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37490
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Actually, that loop is never entered.

Ooh! Good catch. I didn't notice that until you pointed it out.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

There are two conventional forms of the for loop for iterating an arrayNote they include {} even when there is only one line in the body of the statement for reasons of style. Remember: don't write a ; after i++).
There are good reasons for following that convention: those loops always work and never throw an out of bounds exception. Now see whether you can amend those loops to iterate a String.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get poor performance if you use the + operator on Strings repeatedly. You can write
String s = something + somethingElse + anotherThing + somethingDifferent + ... ;
and use the + operator as many times as you like, all in the same expression, and that is the right way to do it. There is an optimisation in the javac tool which sorts that out.
But using + on Strings in a loop slows down execution. You can actually see the delay if you watch after about 10000 iterations. To speed things up, find out about the StringBuilder(←link) class.More about insert() here.
Guess which index you want to insert repeatedly at the first position. Hint: it is the same as that for the first position in an array, so start by trying 0.
 
S.R. Clark
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much guys I changed it to

and it worked great.
I will look into the string builder object as suggested. thanks again!!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!