aspose file tools*
The moose likes Beginning Java and the fly likes taking first four numbers from a set of number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "taking first four numbers from a set of number" Watch "taking first four numbers from a set of number" New topic
Author

taking first four numbers from a set of number

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jul 30, 2008
Posts: 162

You can use for loop from that
Increment index 0 to 3 and get number at that position.

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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 ]

SCJP, SCWCD.
|Asking Good Questions|
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Oct 25, 2005
Posts: 51

Just use the following method in String class.



Kamal Mettananda - SCJP 1.4 (98%)
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Oct 04, 2007
Posts: 518
thank you so much.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

That's because you're using characters as your indexes. These will be converted to their ASCII values.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51


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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: taking first four numbers from a set of number