For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2006 > Regarding the extraction of integers from a String value.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regarding the extraction of integers from a String value.
megha

2006-05-15, 4:16 am

Hello Everyone,
I want to print only the integer values from the String value. So i
used the following method, which returns boolean values. Can you please
tell me how to extract only the Integer values from the String?

public class StrInt{
public static void main(String args[]){
StrInt strint = new StrInt();
strint.hasNumber("agt5678j");
}
public void hasNumber(String s){
System.out.println(s);
for(int j=0;j<s.length();j++){
System.out.println(Character.isDigit(s.charAt(j)));
}
}
}
Thank You !

Gordon Beaton

2006-05-15, 4:16 am

On 15 May 2006 01:41:14 -0700, megha wrote:
> I want to print only the integer values from the String value. So i
> used the following method, which returns boolean values. Can you please
> tell me how to extract only the Integer values from the String?



> public void hasNumber(String s){
> System.out.println(s);
> for(int j=0;j<s.length();j++){
> System.out.println(Character.isDigit(s.charAt(j)));
> }
> }


Your code already extracts the digits from the String, but you throw
them away.

Start by saving the characters that "are Digit", then combine them in
an appropriate manner in order to make numbers. Consider returning a
value from the method, instead of just displaying things.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
megha

2006-05-15, 4:16 am

Yes, I need your help on how to save these digits. I always get
as what to put inside the System.out.println( );
Kindly Help Me !!!
Thank You.

Gordon Beaton

2006-05-15, 8:09 am

On 15 May 2006 02:25:43 -0700, megha wrote:
> Yes, I need your help on how to save these digits. I always get
> as what to put inside the System.out.println( );


System.out.println() has nothing to do with saving the digits, and it
is not part of the solution to the problem you stated in your earlier
post.

The code you posted ALREADY CONTAINS code to extract the digits from
the String. Did you write the code yourself or was it given to you?

I won't do your homework for you. Work through the problem "by hand"
and think about exactly what steps need to be done. Read the API
documentation.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green

2006-05-15, 10:03 pm

On 15 May 2006 02:25:43 -0700, "megha" <divya0106@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>Yes, I need your help on how to save these digits. I always get
> as what to put inside the System.out.println( );
>Kindly Help Me !!!
>Thank You.


see http://mindprod.com/applets/converter.html

ask it how to convert String to int.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
mail2alankar@gmail.com

2006-05-25, 4:16 am

Hi megha
Actually you had almost done the coding .
You only need to do this.

public void hasNumber(String s){
System.out.println(s);
for(int j=0;j<s.length();j++){
if(Character.isDigit(s.charAt(j)))

System.out.println(charAt(j));
}
}
and thats it it will do the work
I hope it help, if it wont let me know.

-Alan

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com