Remove array elements that contain only numbers from string array in Java

A single alphabet, a single digit, symbol, or space within a single quote is known as a character. It requires 2 bytes of memory because in java character is always represented in Unicode format.

Examples of characters:  ‘1’, ‘@’, ‘b’, ‘ ‘, etc.

A single or collection of characters written in the double quote is called a string.

Examples of strings: “CodeSpeedy.com”, “123”, “a”, “5”, “@&”, etc.

An array is a container containing multiple data of the same type within a single variable. It means all data can be of numbers type, string type, etc. In this tutorial, we are going to focus on arrays containing strings only.

An array can be of two types:

  1. Static array/fixed length array: Whose size cannot be changed once created.
  2. Dynamic array/variable length array: size can be changed.
How to declare and initialize an array of strings using the String class:
String s[]={"Hey", "Hey1", "123", "23Hey"};

Steps to follow: Remove array elements that contain only numbers from the string array

  • Create and initialize the array of strings and let named as s.
  • Create two integer type variables let names as l and c and suppose l will store and update the length of each string (use .length() to find the length of a string) on each iteration from the string array and c will count the number of digits in each string and will also be get updated.
  • Iterate through the string array to get one string from the string array index-wise and use .charAt(idx) to get the character from any string at its index idx. It will return the ASCII code of that character and if the ASCII code lies between 48 to 57 it means it is a digit character (since ASCII code of digit 0 to digit 9 is from 48 to 57) then increase the counter c with 1 or else break out of that loop and move on to next string of string array.
  • At the end of the loop if the length of the particular string (l)from the string array and the number of digit characters (c) in that same string from the string array are equal it means that string is the string containing numbers only, then make that string as an empty string since we are dealing with the static string it cannot be resized.
public class Main
{
  public static void main(String[] args) {
    String s[]={"Hey", "Hey1", "123", "23Hey"};
    int l=0,c=0;         //let l will store and update the length of each string on each iteration from the string array and c will count the number of digits in each string and will also be get updated
    for(int i=0;i<s.length;i++)   //It will iterate through the whole string array
    {
        l=s[i].length();    //It will update and store the length of each string from the string array
        c=0;
        for(int j=0;j<l;j++)       //It will iterate through each character of an string from the string array
        {
            if(s[i].charAt(j)<=57 && s[i].charAt(j)>=48)     //It will check if the character is in between the ASCII code from 48-57 (ASCII code of digits 0-9)
            {
                c++;         //counter c will increase by 1 if the character's ASCII code is in between 48-57
            }
            else
            {
                break;        //or else ASCII code is not in between 48-57 it means not a digit .therefore break out from the loop and move on to next string of the string array
            }
        }
        if(l==c)      //If length of the string and the number of digits in that string is equal that means the string is the string with only numbers
        {
            s[i]="";      //update that string with only numbers as null or empty string since in static(fixed length) array we cannot make the cell/space empty
        }
    }
    for(int i=0;i<s.length;i++)      // print the new string array after removing the string containing numbers 
    {
        System.out.print(s[i]+" ");
    }
  }
}

Output:

Hey Hey1  23Hey

Leave a Reply

Your email address will not be published.