Count the number of characters in a given string

Report a typo

You're tasked with creating a simple Java method named 'countCharacters'. This method will read a single string from the standard input. The string would consist of any ASCII character. Your task is to calculate the total number of characters in the string and print it out. For example, if the input string was 'Hello, world!', your program should output '13'. Remember to include this method in the 'main' method of your program.

Sample Input 1:

Hello, world!

Sample Output 1:

13

Sample Input 2:

Hello

Sample Output 2:

5
Write a program in Java 17
import java.util.Scanner;

public class Main {
// Declare the method 'countCharacters'
public static void countCharacters(String inputString) {
// Your code here
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine();

// Call 'countCharacters' method with the input string
countCharacters(inputString);

scanner.close();
}
}

Create a free account to access the full topic