- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Java Program: Count the Number of String Characters in Java
Get A Website Plus a Free Domain Name in Just 1 Hour!
Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!
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:
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(System.in); 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:
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