search
HomeJavaJavaBaseJava implements counting the number of different characters or numbers in a string

Java implements counting the number of different characters or numbers in a string

Dec 12, 2019 pm 12:00 PM
javadifferentnumbercharacterstringaccomplishnumbercalculate

Java implements counting the number of different characters or numbers in a string

Question:

Enter a line of characters according to the prompts. This line of characters can be arbitrary and can include alphanumeric punctuation marks, special symbols, etc. The java program will output you Enter the number of characters of each category in the string.

Result display:

Java implements counting the number of different characters or numbers in a string

Free learning video tutorial recommendation:java video

Code display:

package com.one;
import java.util.*;
public class Flqgs {
	public static Scanner input = new Scanner(System.in);
	public static void main(String[] args) {
		System.out.println("请输入一行字符串:");
		String num = input.nextLine();
		int digital = 0, character = 0, other = 0, blank = 0;
		char [] ch = num.toCharArray();
		
		for(int i=0;i<ch.length;i++){
			if(ch[i] >= &#39;a&#39; && ch[i] <= &#39;z&#39; || ch[i] >= &#39;A&#39; && ch[i] <= &#39;z&#39;){
				character++;
			}else if(ch[i] >= &#39;0&#39; && ch[i] <= &#39;9&#39;){
				digital++;
			}else if(ch[i] == &#39; &#39;){
				blank++;
			}else{
				other++;
			}
		}
		System.out.println("字母个数:"+character);
		System.out.println("数字个数:"+digital);
		System.out.println("空格个数:"+blank);
		System.out.println("其他个数:"+other);
	}
}

Recommended related articles and tutorials: Getting started with java language

The above is the detailed content of Java implements counting the number of different characters or numbers in a string. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor