• 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

taking first four numbers from a set of number

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 162
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use for loop from that
Increment index 0 to 3 and get number at that position.

Thanks
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 51
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just use the following method in String class.

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic