Java Program: Count the Number of String Characters in Java

Java Program: Count the Number of String Characters in Java

How to count the number of characters inside a string in Java Programming?

Counting a number of characters inside a string is not that hard at all. Here we will use another Java predefined function. There are actually number of ways on how to count the characters inside the string but we will just use 1, which is the length(). Have you use length() predefined function before? Apart from length(), we will also use for loop to count the characters. So, the algorithm is like this: Count the characters in the first string entered by the user and add it to the characters of second string entered by the user. Sounds boring? Well, this is just a programming for fun, and there might be others who want to know it, so bear with this. (smile)

Other Related Hubs for String:

Java Source code: Reverse String in Java Using Recursion
Sample Java Program for String Tokenizer
Java Program: Palindrome Test Java Source Code
Java Program: Reverse String in Java Using For Loop

The Java Source Code for Counting the Characters inside a String

package count_the_numberofcharacters_inside_the_string;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
Scanner input = new Scanner(;
int count1 = 0;
int count2 = 0;
System.out.print(“Enter your first name: “);
String first_name = input.nextLine();
System.out.print(“Enter your last name: “);
String last_name = input.nextLine();
for(int i=0; i < first_name.length(); i++) { count1++; } System.out.println("firstname: " +count1); for(int i=0; i < first_name.length(); i++) { count2++; } System.out.println("last_name:"+ count2); System.out.println("Result:"); System.out.println((count1+count2) + " is the sum of the 2 strings above."); } }

Sample Output:

Is this hub useful to you? Please rate it or leave a comment. Happy Programming!

Related Java Tutorials:

5 Important Tips to Learn Java Programming and Other Programming Languages
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples

Java Video Tutorial

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99

Java Books

Head First Java, 2nd Edition, Kathy Sierra, Bert Bates, Sierra Kathy, Bates Be
Current Bid: $19.14
Data Structures and Algorithm Analysis in Java by Mark A. Weiss 3rd ,3e 3ed
Current Bid: $46.48

Other Java Source Codes Samples:

Java source code Sample: Print Different Asterisk Shapes in Recursion
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java Source Code: Binary Search in Recursion
Java source code in Recursive function for X to the power Y
Java Source Code Determine Person’s Salutation and Current Age
Java Code Sample: Sort Numbers in Bubble Sort
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Source Code in a Recursive Linear Search
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source Code Recursion: Recursive Koch Snow Flakes
Java Source Code: Sort Numbers in Selection Sort
Class in Java: Learn More about Java Classes with Sample Java Codes
Java Program: How to Use Switch Statement in Java
Java Program: How to Use If Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming