Answers for "switch case count characters in string"


switch case count characters in string

Scanner keyboard = new Scanner(;
	String name;
	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");

		case 4:
		System.out.println("This name has 4 characters");
Posted by: Guest on November-03-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language