Reputation: 23
I need to do this: Create a program that asks for the user's name and says how many characters the name contains. Your program should be structured so that you put the calculating of the name length in it's own method: public static int calculateCharacters(String text). The tests will be testing both the method calculateCharacters and the program overall.
I know how to count the characters in a string. I just can't figure out how to do this when I have to use a seperate method. Can someone please help me?
import java.util.Scanner;
public class LengthOfName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type your name: ");
String name = reader.nextLine ();
int cal = calculateCharacters(name);
System.out.println(Number of characters: " + cal);
}
public static int calculateCharacters(String text) {
int cal = text.length ();
//System.out.println("Number of characters: " + count ());
return cal;`
Upvotes: 0
Views: 3500
Reputation: 249
What's the error? I'm not seeing any errors in logic here, just a couple syntax issues like missing a quotation mark around "Number of characters: ", a missing curly brace at the end of the calculateCharacters method, and an extra whitespace between reader.nextLine and ().
Your method call itself looks fine.
Upvotes: 2