Question

Help with Java function. Suppose I have a string input that contains letters followed by numbers....

Help with Java function.

Suppose I have a string input that contains letters followed by numbers. Can someone write a function that only returns the number values?

Ex: Input "dog12" should return 12. Edit: It should return 12 as an integer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public static int parseNumbers(String str){
String temp = "";//Temporary empty string to store only digits
for(char ch : str.toCharArray()){
if(Character.isDigit(ch))//Check if character is digit or not
temp += ch;//If yes, then add it to temporary string
}
return Integer.parseInt(temp);//Convert string to int and return
}

SAMPLE PROGRAM OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Help with Java function. Suppose I have a string input that contains letters followed by numbers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT