• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

StringBuffer question

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all,

I have a very basic question...

Consider the following code


I am using insert(int offset,int char) here. My question is what is offset exactly. I understand how it functions, but in this case it went where indexOf would have given ArrayIndexOutOfBounds exception...
I understood offset is 1 based and index is 0 based.
But with all my understanding of offset, c should have pushed back space after insert satement.ie. space should have been after c.

I am not getting this minor thing.
Please help...
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at the Javadoc, it seems fairly straight-forward.

One thing in your post: offset is not 1-based. Offset may range from [ 0, N ] where N is the length of the string buffer.
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi M Easter,

Can you please paste the link to the definition here.
I have searched for it with keywords "java api offset definition"
" javadoc offset" " java api offset" , but I cant seem to find
the definition of offset.

Also does it mean that even though I dont have a index position N in a string of lenght N, I can still insert at position N, using offset.?

Thanks,
Megha
[ May 01, 2007: Message edited by: megha joshi ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java API. (Find "StringBuffer" in the lower left frame.)
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!
Always remember,
offset, begin, index - are all 0 based.
end, position - are all 1 based.

Usually, the first argument in such methods will be 0 based and the second argument will be 1 based.

Hth,
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, and start is also 0-based.

apologies if I am wrong Pasha, the foll statement is deviating

Originally posted by Faisal Pasha:

Usually, the first argument in such methods will be 0 based and the second argument will be 1 based.

Hth,


if you take
StringBuffer sb=new StringBuffer("abcbdebf");
int i=sb.lastIndexOf("b",6);
o/p : 6
here the second argument is also 0-based
what you say?

Thanks
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,

StringBuffer s = new StringBuffer("012345678 ");
System.out.println(s.length()); //10
s.insert(10, "c");


> 0 1 2 3 4 5 6 7 8 <
| | | | | | | | | | |
0 1 2 3 4 5 6 7 8 9 10 "c"

"There was space in the 10th char(9th index)"
s.indexOf(10); //returns "c" what you inserted at 10th index


When you specify the index that is beyond the length of the characters
StringIndexOutOfBoundsException is thrown.
It applies to negative and positive values.

Thanks for posting this question!
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

what about my previous post is it right or wrong?
please help to specify some comments on previous comments, either agreeing with the already posted answers or not. whatever it may be wrong/right please specify it.
if anyone posts wrong answers that couldn't be taken as correct.
since it is a discussion forum I'll be expecting comments about my answers and thoughts about the question.

Thanks.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Madhu,

You are right!

The second argument of the lastIndexOf() is known as fromIndex. It means the search should start from that index.

Note:
1- If you specify the fromindex, that is not helpful to get the result,
it returns -1;
2- If you specify any number greater that the max index, the output would be
obvious the lastIndexOf(...) is intended for. There is not IndexOutOfBoundsException or such.



Thanks,
[ May 01, 2007: Message edited by: Chandra Bhatt ]
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra,

your answers are really helpful.
when we use
sb.lastIndexOf('b',932);//it is working as sb.lastIndexOf('b');
am I right?
Really good knowledge on StringBuffer.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by madhu v pe:
Thanks Chandra,

your answers are really helpful.
when we use
sb.lastIndexOf('b',932);//it is working as sb.lastIndexOf('b');
am I right?
Really good knowledge on StringBuffer.



Yeah Madhu,

We can look at the needful line of the lastIndexOf() method of the String class that is also used by StringBuffer:

Excerpts from the String class's lastIndexOf(...)


[EDITED] I made typo while writing lastIndexOf(). There is no method or such "lastIndexOfMethod(...)". Sorry for inconvenience.
[ May 02, 2007: Message edited by: Chandra Bhatt ]
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:


Excerpts from the String class's lastIndexOfMethod(...)


Pleae if you don't mind can you explain this.
and where is this lastIndexOfMethod()

By the By are you preparing for SCJP 1.5? which books are you following.
I am planning to write on june 2nd,
I am following Complete reference for basics and K&B for exam oriented.
I haven't scheduled my exam till now bcos of lack of confidence, I have given full reading once, now going to each and every topic working on it.
still I am not confident.
suggestions will be appreciated.

Thanks
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Actually the offset argument does'nt really start from 1
The offset argument must be greater than or equal to 0, and less than
or equal to the length of this string buffer.
As Chandra said the value that we print at s.charAt(11) throws exception though the string buffer length increases by 1.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhu,


See my edited post. I am sorry for typo.
Anyways I am on the same boat as you are, going to write the exam 310-055 on 14th May. K&B is the best. I can't dare to say "if and but" regarding that book.

It is proven! Go with it.

You are doing good as I see. Our target is to get certified.

Cheers!!!
[ May 02, 2007: Message edited by: Chandra Bhatt ]
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lalitha,

you are right,
if you see the below method
offset is the start index of str array which can be >=0 and <=len-1
len is the no of characters to pickup which is <= length of the array
append(char[] str, int offset, int len)
[ May 02, 2007: Message edited by: madhu v pe ]
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lalitha,

please look into the messages your same message has been posted for 4 times.

Thanks
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for helping everyone..Now I get it.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by madhu v pe:
Lalitha,

please look into the messages your same message has been posted for 4 times...


I left the first one, and removed the duplicates.
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic