How to check if there is only one time zero in a string with Java

1 Answer

0 votes
public class MyClass {
    private static boolean OnlyOneZero(String str) {
        int count = 0;
    	
    	for (char ch : str.toCharArray()) {
    		if (ch == '0') {
    			count++;
    		}
    	}
    	
    	return count == 1;
    }
    
    public static void main(String args[]) {
	    final String s1 = "294098";
	    System.out.println(OnlyOneZero(s1) ? "yes" : "no");

	    final String s2 = "47034099";
	    System.out.println(OnlyOneZero(s2) ? "yes" : "no");
    }
}





/*
run:
 
yes
no
 
*/

 



answered Apr 11, 2023 by avibootz

Related questions

1 answer 138 views
1 answer 127 views
1 answer 114 views
1 answer 129 views
1 answer 118 views
...