Вот мой код:
import javax.swing.*;
public class Vowel
{
public static void main(String[] args)
{
String myString = JOptionPane.showInputDialog(null,"Enter your text: ");
char[] cArray = myString.trim().toCharArray();
if(cArray.length == 0)
{
JOptionPane.showMessageDialog(null, "You did not enter any text");
return;
}
String vowelLetters = "Vowel Letters: [";
String notVowelLetters = "Non-Vowel Letters: [";
int nVowel = 0;
int nNotVowel = 0;
for(int i = 0; i < cArray.length; i++)
{
if(isVowel(cArray[i]))
{
vowelLetters += "" + cArray[i];
nVowel++;
if(i != cArray.length - 1) vowelLetters += ", ";
continue;
}
notVowelLetters += "" + cArray[i];
nNotVowel++;
if(i != cArray.length - 1) notVowelLetters += ", ";
}
JOptionPane.showMessageDialog(null, "Your text Contains:\n" + vowelLetters + "] which contains " + nVowel + " Letters\n" + notVowelLetters + "] which contains " + nNotVowel + " Letters");
}
public static boolean isVowel(char c)
{
return Character.toUpperCase(c) == 'A' || Character.toUpperCase(c) == 'O' ||
Character.toUpperCase(c) == 'U' || Character.toUpperCase(c) == 'I' ||
Character.toUpperCase(c) == 'E' ? true : false;
}
}
если я введу, например, следующий текст: "abcdef"
, вывод будет таким:
Your text contains:
Vowel Letters: [a, e, ] which contains 2 letters
Non-Vowel Letters: [b, c, d, f] which contains 4 letters
Как видите, в [a, e,] есть дополнительный знак ",". Я хочу, чтобы он был [a, e]
Как я мог решить эту проблему?