• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings problem..

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be written to the standard output when the following program is run?

Options :
1)25
2)24
3)23
4)22
5)None of the above
Answer :5) None of the above
But I feel that the answer is 2)24 becauase the length of the word "hello" is 5, plus 2 blanks i.e 7 plus "world" ie12. and in the end we are calling the same thing twice , i.e 12+12 makes it 24.
Can any one explain me why am I wrong?
Sonir
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir
Try to compile and run this and see what you get. This should anwer your question.
Remember the println is the simplest and most of the time the only debugging tool you need.

HTH
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shivaji,
I tried running the program .Its giving me an output of 13 and thats why I am confused..
For which I think it should be 24...
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
here when we do concat() it doesnt change the string line as we didnt return anything. so, we didnt have 'world' concatenated with "<space> hello<space><space>". if we do trim() now then it trims the line and it becomes "hello" which is assigned to the new string newLine.
so we have line = "<space>hello<space><space>" (len = 7)
newLine = "hello" (len = 5)
total len = 7+5 = 12.
regards
maulin.
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir :
If you had put the println statements before and after the call to the Concat, you would have realized that the call to concat did not really change the String stored for line.
Therefore line remained as Hello followed by three spaces.
Hope this helps
reply
    Bookmark Topic Watch Topic
  • New Topic