0
Q:

switch case count characters in string

Scanner keyboard = new Scanner(System.in);
//string
	String name;
//int
	int stringSize;
	System.out.print("Enter a name: ");
	// the name entered
	name = keyboard.nextLine();
	// counting the lenght of the name and making it an int
	stringSize = name.length();
	// switching with the int value not the string
	switch (stringSize)
	{
		case 1:
		case 2:
		case 3:
		System.out.println("This name has 3 or less characters");
		break;

		case 4:
		System.out.println("This name has 4 characters");
		break;
    }
0

New to Communities?

Join the community