In the previous article, we have seen Java Program to Calculate Standard Deviation
In this article we are going to see how to Count Strings and Integers from an Array using Java programming language.
Java Program to Count Strings and Integers from an Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to count strings and integers from an array.
Method-1: Java Program to Count Strings and Integers from an Array By Static Initialization of Array Elements
Approach Using try-catch:
- Method
Integer.parseInt()
throws “NumberFormatException” – if the string does not contain a parsable integer, we can take advantage of this to solve this problem. - Initialize two variables
stringCount = 0
andintCount = 0
. - Iterate over the array.
- In try block use
Integer.parseInt()
to check if the ith element is an integer or not. - If no then it’ll move to catch block and we can increment the stringCount variable.
- If yes, it will continue to run inside the try block and we can increment intCount variable.
Program:
import java.util.Arrays; public class Main { public static void main(String arg[]) { // enter string u want here. String x[] = { "Raj", "77", "101", "99", "Jio" }; countIntnString(x); } private static void countIntnString(String[] arr) { int intCount = 0, stringCount = 0; // scan the string. for (String i : arr) { // check if string is integer or not using try catch try { Integer.parseInt(i); intCount++; } catch (NumberFormatException e) { stringCount++; } } // show the numeric and string value after counting. System.out.println("Array \"" + Arrays.toString(arr) + "\" contains " + intCount + " integers and " + stringCount + " strings."); } }
Output: Array "[Raj, 77, 101, 99, Jio]" contains 3 integers and 2 strings.
Method-2: Java Program to Count Strings and Integers from an Array By Dynamic Initialization of Array Elements
Approach:
- Create scanner class object.
- Ask use length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Iterate over the array.
- Check if the element is numerical using regex.
- If yes, increment intCount by one.
- If no, increment stringCount by one.
Program:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String arg[]) { // enter string u want here. // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the size of array: "); int n = sc.nextInt(); // extra Scanner.nextLine() to consume the newline character due to // enter key else it will skip the next nextLine() inside the loop. sc.nextLine(); // initialize array with size n String[] arr = new String[n]; // take input from user for array elements System.out.println("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextLine(); } countIntnString(arr); } private static void countIntnString(String[] arr) { int intCount = 0, stringCount = 0; // check if the element is numerical for (String i: arr) { if (i.matches("[0-9]+")) { intCount++; } else { stringCount++; } } // show the numeric and string value after counting. System.out.println("Array \"" + Arrays.toString(arr) + "\" contains " + intCount + " integers and " + stringCount + " strings."); } }
Output: Enter the size of array: 5 Enter array elements: raj 66 abc 90 jar Array "[raj, 66, abc, 90, jar]" contains 2 integers and 3 strings.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Related Java Programs: