Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to count strings and integers from an array of strings in Java

1 Answer

0 votes
public class Program {
    static int countnum = 0, countstr = 0;
    
    static void count_strings_and_integers(String arr[]) {
        int len = arr.length;

        for (int i = 0; i < len; i++) {
            try {
                int num = Integer.parseInt(arr[i]);
                countnum++;
            } catch (NumberFormatException e) {
                countstr++;
            }
        }
        
        
    }
    
    public static void main(String[] args) {
        String arr[] = {
              "java",
              "888",
              "9",
              "c",
              "python",
              "109",
              "c++"
        };
        
        count_strings_and_integers(arr);
        
        System.out.println("Numbers: " + countnum + "\nStrings: " + countstr);
    }
}




/*
run:

Numbers: 3
Strings: 4
  
*/

 



answered Feb 17, 2024 by avibootz
edited Feb 17, 2024 by avibootz

Related questions

1 answer 103 views
1 answer 149 views
1 answer 86 views
1 answer 153 views
...