Home » Java programs » Java array programs

Java program to count strings and integers from an array

In this java program, we have an array with combinations of strings and integers, and program will count total number of strings are integers.
Submitted by Chandra Shekhar, on January 15, 2018

Given an array with strings and integers and we have to count strings and integers using java program.

Example:

    Input:
    Array = {"Raj", "77", "101", "99", "Jio"}

    Output:
    Numeric:3
    Strings:2

Java program:

package ExceptionHandling;

public class ExExceptionToCountStringAndNumericValues
{    
	public static void main(String arg[])
	{
		// enter string u want here.
		String x[]={"Raj","77","101","99","Jio"};
		int cn=0,cs=0;

		//print array elements
		System.out.println("Array elements are: ");
		for(int i=0; i<x.length; i++){
			System.out.println(x[i]);
		}

		// scan the string.
		for(int i=0; i<x.length; i++)
		{
			try
			{
				int j=Integer.parseInt(x[i]);
				cn++;
			}
			catch(NumberFormatException e)
			{
				cs++;
			}
		}
		// show the numeric and string value after counting.
		System.out.println("Numeric:"+cn+"\nStrings:"+cs);
	}
}

Output

First run:
Array elements are: 
Raj
77
101
99
Jio
Numeric:3
Strings:2

Second run (with different input)
Array elements are: 
Gwalior
77
Indore
99
Bhopal
Numeric:2
Strings:3






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.