| Author |
String and StringBuffer
|
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
this question is from sun_certifyme_310-055 by taylor(visual cert Simulator)
1 .class Cert6 {
2. public static void main(String[] args) {
3. StringBuffer sb=new StringBuffer("123456789");
4. // insert code here
5. System.out.println(sb);
6. }
7. }
when i try to insert at line 4 the following code :
StringBuffer sb=new StringBuffer("123456789");
sb.substring(3,6).delete(1,3).insert(1,"24");
i must get the output as 4247 as per the simulator.. but when i try to execute the code i am no able to compile the code:i get the error telling cannot find symbol delete(int,int)
location java.lang.String
what might be the problem. i also feel that i must get a different output
i must get 424 as the output as per my logic.. but the exam simulator is telling that the output is 4247;; and asking for the code option.?
|
Thanks & Regards,
shaad
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
|
also the fact that delete method can be invoked on StringBuffer ;;
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Dear Shahid,
It won't compile even. There is no method like delete in the StringBuffer class and even no method like insert.
Please do check API documentation. No such method exists thats why it is giving you this error.
Hope this helps,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
I think so, there is something wrong with the example which you have copied.
Please cross check.
Best Regards,
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
|
seems like there is no delete and insert methods in string buffer but in K&B 310-055 these methods are mentioned..
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Can you point out the chapter and topic?
Better if you can copy what is exactly written in K&B as many people won't be having a book at hand.
Best Regards,
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
chapter no-6
page 426;
topic-important methods in StringBuffer and StringBuilder
2006 released book
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Dear Shahid,
K&B is talking about StringBuilder not StringBuffer.
Hope this helps,
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
StringBuilder sb=new StringBuilder("0123456789");
System.out.println(sb.delete(4,6)); // output is "01236789"
in K&B 310-055
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Dear Shahid,
insert and delete are present in String Builder not StringBuffer. Straight from K&b
Hope this helps you.
Happy Preparation,
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
True,
But your mock exam question is referring to StringBuffer not StringBuilder, so i believe the option should be
compilation fails.
Best Regards,
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
|
yea.. t\you are right sehgal.. but i tried StringBuilder also.. it isnt executing.showing the same error.. and also that methods of StingBuilder and StringBuffer are same except that StringBuffer methods are thredsafe
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
|
and there is no compiler fails oiption too.. please wait a minute.. i will write down the options
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
It is showing the error because, if you check the API documentation, subString() in StringBuilder returns a String
and class String doesn't have any insert or delete methods.
Hope this helps,
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
1. class Cert6 {
2. public static void main(String[] args) {
3. // insert code here
4. System.out.println(sb);
5. }
6. }
which 2 code fragments inserted independently at line 3 generate the output-4247
A. String s="123456789";
s=(s-"123").replace(1,3,"24")-"89";
B. StringBuffer s=new StringBuffer("123456789");
C. delete(0,3).replace(1,3,"24").delete(4,6);
D. StringBuffer s=new StringBuffer("123456789");
E. substring(3,6).delete(1,3).insert(1,"24");
F. StringBuilder s=new StringBuilder("123456789");
G. substring(3,6).delete(1,2).insert(1,"24");
H. StringBuilder s=new StringBuilder("123456789");
I. delete(0,3).delete(1,3).delete(2,5).insert(1,"24");
and its given as B and E as the correct options
you are right .that might be the correct reson.but what about the options that i mentioned here.. none of them are correct then
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Dear Shahid,
Yes shahid, none of them are correct.
I believe this question has wrong options. If you think carefully, you are invoking subString() on what instance.
It should atleast refer to some reference variable like sb. Is that making a bit of sense?
Best Regards,
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
you are right prithvi. options are wrong.and even if we try to compile it on StringBuffer object, substring() returns String and so we get compiler error later when we try to invoke delete().
thanks prithvi...
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Dear Shaid,
You are most welcome.
Happy Preparation,
|
 |
 |
|
|
subject: String and StringBuffer
|
|
|