Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

taking first four numbers from a set of number

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to get first four number from 14 numbers .I can able to get last 4 numbers using substring.But i am unable to get first four numbers.anyone please assist me to do this.

Thanks.
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use for loop from that
Increment index 0 to 3 and get number at that position.

Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to use a loop. Assuming the "number" is infact a String (which I'm infering by preethi Ayyappan saying they can get the last 4 digits with substring); the String class has a substring method that takes a begin and end index. Just use that.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can able to get last 4 numbers using substring

You mean to say that the set is a String?
If yes then what is the problem in getting first four characters

Paul beat me on that by a minute
[ August 13, 2008: Message edited by: Amit Ghorpade ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
I have used substring to get last 4 numbers like the following:

the output will be 7890.But i am unable to get first 4 numbers.please assist me in code how can we use the same substring for this too.
[ August 13, 2008: Message edited by: preethi Ayyappan ]
 
Kamal Mettananda
Ranch Hand
Posts: 51
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use the following method in String class.

 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written the following code:

I am getting the following error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 49
at java.lang.String.substring(String.java:1935)
at CurrentTime.main(CurrentTime.java:13)


 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you're using characters as your indexes. These will be converted to their ASCII values.
 
Kamal Mettananda
Ranch Hand
Posts: 51
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int beginIndex='c';
int endIndex='3';


I guess this is a mistake. You have used character 'c' instead of 0. Shouldn't it be;
int beginIndex = 0;
[ August 13, 2008: Message edited by: Kamal Mettananda ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic