GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Java Program to Extract Numbers from String
Java Program to Extract Numbers from StringThis article is created to cover a program in Java that extracts all numbers from a string entered by user at run-time of the program. Extract Numbers from String - Basic VersionThe question is, write a Java program to extract numbers from a string. The string must be received by user. Here is its answer: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { String str; char ch; Scanner s = new Scanner(System.in); System.out.print("Enter the String: "); str = s.nextLine(); System.out.println("\nNumbers available in the String are:"); for(int i=0; i<str.length(); i++) { ch = str.charAt(i); if(Character.isDigit(ch)) System.out.print(ch); } } } The snapshot given below shows the sample run of above Java program with user input codescracker@123 as string to extract all numbers available in it: Extract Numbers from String - Complete VersionThe problem with above program is, if number does not found in the string, then still the program produce the message Numbers available in the String are:, with no number. That looks weird. Also the program actually does not stores the extracted number from the string into another variable, as the program only prints the number at the time of checking using isDigit(), character by character. With keeping all the limitations of above program in mind, I've created another modified version of the program, as given below. This program I think provides a good user experience too: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { String str; char ch; int strLen, i, strIndex=0; Scanner s = new Scanner(System.in); System.out.print("Enter the String: "); str = s.nextLine(); strLen = str.length(); char[] nums = new char[strLen]; for(i=0; i<strLen; i++) { ch = str.charAt(i); if(Character.isDigit(ch)) { nums[strIndex] = ch; strIndex++; } } if(strIndex==0) System.out.println("\nNumber not found in the string!"); else if(strIndex==1) { System.out.println("\nThere is only one number found in the string."); System.out.println("And the number is: " +nums[0]); } else { System.out.println("\nNumbers found in the string are:"); for(i=0; i<strIndex; i++) System.out.print(nums[i]); } } } Here is its sample run with user input Java is Fun! Here is another sample run with user input Java is Fun@1234... « Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |