स्ट्रिंग में प्रत्येक कैरेक्टर की घटनाएं गिनना
यह उदाहरण दर्शाता है कि Android में स्ट्रिंग में प्रत्येक कैरेक्टर की घटनाओं (occurrences) को कैसे गिना जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText android:id="@+id/name" android:layout_width="match_parent"
android:hint="नाम दर्ज करें" android:layout_height="wrap_content" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="@+id/save" android:text="सेव करें"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<TextView android:id="@+id/textview" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
चरण 3: MainActivity.java
package com.example.myapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
EditText name;
TextView textview;
Map<Character, Integer> charCountMap = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
textview = findViewById(R.id.textview);
findViewById(R.id.save).setOnClickListener(v -> {
String input = name.getText().toString();
if (!input.isEmpty()) {
charCountMap.clear(); // पिछली गिनती साफ़ करें
char[] chars = input.toCharArray();
for (char c : chars) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
// परिणाम को सुंदर फॉर्मेट में दिखाएं
StringBuilder sb = new StringBuilder();
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
textview.setText(sb.toString());
Toast.makeText(this, "गिनती पूरी", Toast.LENGTH_LONG).show();
} else {
name.setError("नाम दर्ज करें");
}
});
}
}
ऐप चलाएं
डिवाइस कनेक्ट करें और ऐप चलाएं।

परिणाम में, हम TextView में प्रत्येक कैरेक्टर की घटनाओं की संख्या प्राप्त कर रहे हैं। HashMap कैरेक्टर को कुंजी और गिनती को मान के रूप में स्टोर करता है।