Input is "Listen, Mr. Jones, calm down."
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
int numPun = 0; //number of punctiation and spaces
// Add more variables as needed
userText = scnr.nextLine(); // Gets entire line, including spaces.
for (int i = 0; i < userText.length(); i++){
if ((userText.charAt(i) != ' ') && (userText.charAt(i) != ',') && (userText.charAt(i) != '.'));{
numPun++;
}
}
System.out.println(numPun);
}
}
My current output is to 29, which is the total amount of characters in the whole line. Expected output is supposed to be 21. Where am i going wrong here?
if
, and use &&
NOT ||
In a previous version of this answer I specified to use ||
instead of &&
. DO NOT do that. Kudos to Basil Bourque's answer for being the first one to point that out.
Use:
for (int i = 0; i < userText.length(); i++){
char current = userText.charAt(i);
if (current != ' ' && current != ',' && current != '.'){
numPun++;
}
}
You have an extra ;
after the if
statement expression.
See it in action
Explanation:
In the previous version of this answer the expression current,= ' ' || current.= ',' || current != '.'
current,= ' ' || current.= ',' || current != '.'
is equal to if current is not a space or is not a comma or is not a period do this:
.
That meant that if the character was a comma, it would add 1 to numPun
, because it was not a space.
In the new expression, it is equal to if current is not a space and is not a comma and is not a period
. Therefore it would first check whether it was a space, then a comma, then a period.
||
noAs Answer by Spectric correctly says, you have stray semicolon to fix.
But the instruction to use ||
is incorrect.
Here is your code, corrected.
String input = "Listen, Mr. Jones, calm down.";
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
//System.out.println( "c = " + c );
if ( ( c != ' ' ) && ( c != ',' ) && ( c != '.' ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
See this code run live at IdeOne.com .
21
I would have written such code by using a List
of forbidden characters rather than a series of equality tests.
String input = "Listen, Mr. Jones, calm down.";
List < String > nonLetters = List.of( " " , "," , "." );
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
System.out.println( "c = " + c );
if ( ! nonLetters.contains( String.valueOf( c ) ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
Even better: Use code points rather than char
type.
char
The char
type is obsolete in Java. That type is a relic, unable to represent even half of the characters defined in Unicode .
Instead, work with code point number to represent each character. Unicode assigns one integer number to each of its 143,859 characters. The code points range from zero to over a million, most being unassigned.
The String
class can return an IntStream
of the code point number for each character. Convert that IntStream
to an array of int
numbers. Loop the array. For each code point number, ask if the character Unicode assigns to that number is considered a letter or not. If a letter, increment our count.
String input = "Listen, Mr. Jones, calm down.";
int countLetters = 0;
int[] codePoints = input.codePoints().toArray();
for ( int codePoint : codePoints )
{
if ( Character.isLetter( codePoint ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
21
We can even go so far as to make a one-liner using Java stream and lambda features.
long countLetters =
"Listen, Mr. Jones, calm down."
.codePoints()
.filter(
( int codePoint ) -> Character.isLetter( codePoint )
)
.count()
;
21
(userText.charAt(i).= ' ') || (userText,charAt(i).= '.') || (userText.charAt(i) != '.')
also, missing the && (c.= '!')) which was counting all extra exclamations and failing the tests.
completed code as follows:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
userText = scnr.nextLine();
String input = userText;
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
if ( ( c != ' ' ) && ( c != ',' ) && ( c != '.' ) && (c != '!' ) && (c !=
'!' ))
{
countLetters++;
}
}
System.out.println(countLetters );
}
}
Often, the best first step is to simplify the problem: if rephrased, it can become “how many words characters are there?”, which can be expressed in code as “how long is the input after removing all non-word chars:
int count = userText.replaceAll("\\W", "").length();
being just one line of code, there are less lances for bugs to hide.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.