Given a line of text as input, output the number of characters
excluding spaces, periods, or commas.
Ex: If the input is:
the output is:
Note: Account for all characters that aren't spaces, periods, or
commas (Ex: "r", "2", "!").
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
int charCount =0;
userText = scnr.nextLine(); // Gets
entire line, including spaces.
for (int i = 0; i < userText.length();
i++){
if (
(userText.charAt(i)>='a'&& userText.charAt(i)<='z')||
(userText.charAt(i)>='A'&&
userText.charAt(i)<='Z'))
{
charCount++;
}
}
System.out.println(charCount);
}
}
I keep missing something
4.15 LAB: Count input length without spa Given a line of text as input, output the number of characters excluding spaces, periods, or commas. Ex: If the input is: Listen, Mr. Jones, calm down. the output is: 21 Note: Account for all characters that aren't spaces, periods, or commas (Ex: "r", "2", "!").
LabPro...m.java Load default template... Nm 0 1 import java.util.Scanner; 2 3 public class LabProgram { 4 public static void main(String[] args) { 5 Scanner scnr = new Scanner(System.in); 6 String userText; 7 int charCount =0; 8 9 userText scnr.nextLine(); // Gets entire line, including spaces. 10 11 12 13 14 15 16 17 18 19 } for (int i = 0; i < userText.length(); i++){ if ( (userText.charAt(i)>='a'&& userText.charAt(i)<='z')|| (userText.charAt(i)>='A'&& userText.charA { charCount++; } } System.out.println(charCount); }
1: Compare output ^ Input Listen, Mr. Jones, calm down. Your output 21 2: Compare output ^ Output differs. See highlights below. Input Howdy! Your output Expected output 6 3: Compare output ^ Input abcd, 1,,efgh....ijkl Your output 12
Given a line of text as input, output the number of characters excluding spaces, periods, or commas. Ex: If the input is
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am