Reputation: 41
I have a Java assignment where I need to read in a Character and then count the number of times that character appears in the array. This is what I have so far.
import javax.swing.JOptionPane;
public class ArraySring
{
public static void main(String args[])
{
String userChar;
userChar = JOptionPane.showInputDialog("Enter a character");
String dow[] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
for(int x=0; x<7; x++) {
System.out.println(dow[x]);
}
System.out.println();
}
} // end ArrayStrings
Upvotes: 1
Views: 110
Reputation: 338740
char
Avoid the char
type, as it is now legacy. That type was invented before Unicode expanded beyond its original 64K limit. Unicode now defines more than twice that many characters: 143,859 characters in Unicode 13. So using char
will fail when encountering a character from beyond the Basic Multilingual Plane (the first 64,000 characters, approximately).
In modern Java, we should be working with code point numbers rather than char
values.
As an example, let us pretend the emoji character FACE WITH MEDICAL MASK is used when spelling certain day-of-week names. Silly, but good for a demonstration. That character in Unicode is assigned to the code point 128,567.
This code uses an IntStream
. Streams are another way to represent a series of values in Java. Not important to you now as a beginner learning Java. Just know that we can make an array from the series of values provided by a stream, by calling string.codePoints().toArray()
. We end up with an array of int
integer numbers. Each int
integer number is the code point of a character in the text of a String
.
String userCharacter = "😷";
String strings[] = { "M😷nday" , "Tuesd😷y" , "Wednesday" };
int count = 0;
for ( String string : strings ) // Loop through the day-of-week names.
{
IntStream codePoints = string.codePoints(); // Get a stream of `int` integer numbers, the Unicode code point number for each character in the string.
int[] ints = codePoints.toArray(); // Convert stream to an array.
for ( int i : ints ) // Loop through each number, each code point.
{
System.out.println( "i = " + i + " character: " + Character.toString( i ) ); // Debugging. Dump values to the console.
if ( i == userCharacter.codePointAt( 0 ) ) // Compare this nth number (the code point of the nth character in the day-of-week name) against the code point number of the character given by the user.
{
System.out.println( "Hit.") ; // Debugging.
count++; // If they match, increment our counter.
}
}
}
Dump to console.
System.out.println( "count = " + count ); // Dump to console.
System.out.println( "userCharacter.codePointAt( 0 ): " + userCharacter.codePointAt( 0 ) );
When run.
i = 77 character: M
i = 128567 character: 😷
Hit.
i = 110 character: n
i = 100 character: d
i = 97 character: a
i = 121 character: y
i = 84 character: T
i = 117 character: u
i = 101 character: e
i = 115 character: s
i = 100 character: d
i = 128567 character: 😷
Hit.
i = 121 character: y
i = 87 character: W
i = 101 character: e
i = 100 character: d
i = 110 character: n
i = 101 character: e
i = 115 character: s
i = 100 character: d
i = 97 character: a
i = 121 character: y
count = 2
userCharacter.codePointAt( 0 ): 128567
For more info, see:
Upvotes: 3
Reputation: 5067
Nice response by Basil, thank you for the comprehensive description. Taking Basil's advice on using code points instead of chars and on top of that using some stream processing also will transform the solution into:
public static void main(String args[]) {
int inputCodePoint = JOptionPane.showInputDialog("Enter a character").toLowerCase().codePointAt(0);
String dow[] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
System.out.println(Arrays.stream(dow)
.flatMap(s -> Arrays.stream(s.split("")))
.filter(s -> s.equals(Character.toString(inputCodePoint)))
.count());
}
This made use of Character.toString(int) added in JDK 11.
Upvotes: 1
Reputation: 1579
Here you go:
public static void main(String args[]) {
char userChar = JOptionPane.showInputDialog("Enter a character").toLowerCase().charAt(0);
int count = 0;
String dow[] = // ...
for (String str : dow)
for (char ch : str.toLowerCase().toCharArray())
if (ch == userChar)
count++;
System.out.println(count);
}
Upvotes: 1