switch case count characters in string

Solutions on MaxInterview for switch case count characters in string by the best coders in the world

showing results for - "switch case count characters in string"
Alina
15 Mar 2019
1Scanner keyboard = new Scanner(System.in);
2//string
3	String name;
4//int
5	int stringSize;
6	System.out.print("Enter a name: ");
7	// the name entered
8	name = keyboard.nextLine();
9	// counting the lenght of the name and making it an int
10	stringSize = name.length();
11	// switching with the int value not the string
12	switch (stringSize)
13	{
14		case 1:
15		case 2:
16		case 3:
17		System.out.println("This name has 3 or less characters");
18		break;
19
20		case 4:
21		System.out.println("This name has 4 characters");
22		break;
23    }