codescracker
java

Java Program to Extract Numbers from String



« Java Tutorial Java Examples »

This 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 Version

The 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:

java extract numbers from string

Extract Numbers from String - Complete Version

The 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!

extract numbers from string java

Here is another sample run with user input Java is Fun@1234...

java extract number from string program

Java Online Test


« Java Tutorial Java Examples »



© Copyright 2021. All Rights Reserved.

CodesCracker