Given a line of text as input, output the number of characters excluding spaces, periods, exclamation points, 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, exclamation points, or commas (Ex: "r", "2", "?").
Question:
Given a line of text as input, output the number of characters excluding spaces, periods, exclamation points, 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, exclamation points, or commas (Ex: "r", "2", "?").
My incorrect code:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
int counter = 0;
userText = scnr.nextLine();
for (int i = 0; i < userText.length(); i++) {
if (userText.charAt(i) != ' ' && userText.charAt(i)!= '.' && userText.charAt(i) != ',') {
counter++;
}
}
System.out.println(counter);
}
}
example/hint i'm given to compare to my code as to why i'm getting it wrong:
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images