Unreproducible
As commented by Charlie Armstrong your problem is not reproducible. You should provide a MCVE.
Your if
cascade is faulty. The first if
tests for wordCount
being less than two.
if (wordCount < 2) { // <--- Condition met for count of 0 or 1.
Zero is less than two. So the first test succeeds, and its code runs.
Your second if
test for less than zero is redundant. That second if
:
else if (wordCount == 0) { // <--- Never reaches this point for 0 or 1.
…will never execute for a wordCount
of 0 or 1. The first test if (wordCount < 2) {
will be true for a wordCount
of zero (or one), so its System.out.println
will run, and the if
cascade ends. So we never have a chance to test if the count is zero.
For your rule of "if there are 0 n's in that word, it should display 0 n", change your code to:
if ( wordCount > 1 ) { // For a count of 2 or more.
System.out.println(wordCount + " " + userLetter + "'s");
}
else { // Else use singular for count of 0 or 1. (Ignoring negative numbers.)
System.out.println(wordCount + " " + userLetter);
}
As commented by Stephen C, you should be using the plural with both zero and with more than one. So your logic could be reduced checking if the count is not one, in which case you append the plural s
. See code example further down in this Answer.
By the way, we can replace much of your code by letting Java do the heavy lifting via Collections.frequency
. Read on.
Avoid char
The char
type is obsolete, unable to represent even half of the characters defined in Unicode.
Unicode code point
Instead, learn to use Unicode code point integer numbers when working with individual characters.
You can turn a string into an IntStream
to get a series of int
values, each value being the code point of a character in that string.
Call boxed
to convert each streamed int
primitive into a Integer
object.
List< Integer > codePoints = "Java".codePoints().boxed().toList() ; // Prior to Java 16, use `.collect( Collectors.toList() )` in place of `.toList()`.
Integer codePoint = "a".codePoints().toArray()[0] ;
int frequency = Collections.frequency( codePoints , codePoint ) ;
frequency = 2
If result is other than one, append s
Report result.
As Stephen C commented, in proper English your target letter should be wrapped in quotes, ideally curly-quotes, without any use of an apostrophe. Append an s
for a count of zero or more than one, but omit the s
for a result of one.
String output = String.valueOf( frequency ) + " ‘" + Character.toString( codePoint ) + "’" ;
if ( frequency!= 1 ) {
output = output + "s" ;
}
See this code run live at IdeOne.com.
2 ‘a’s
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.* ;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
List< Integer > codePoints = "Java".codePoints().boxed().collect( Collectors.toList() ) ;
Integer codePoint = "a".codePoints().toArray()[0] ;
int frequency = Collections.frequency( codePoints , codePoint ) ;
String output = String.valueOf( frequency ) + " ‘" + Character.toString( codePoint ) + "’" ;
if ( frequency!= 1 ) {
output = output + "s" ;
}
System.out.println( output ) ;
}
}