I want the user to write a text and then the program is gonna check how many vowels, consonants and punctuations the text has. When the user type numbers in the text my program counts them as consonants and i do not want that. I want to print out a error message when the user type numbers in the text.
This is my code so far:
import java.util.Scanner;
public class Exercise7 {
private static Scanner input;
public static void main(String[] args) {
System.out.println("Write a text");
input = new Scanner(System.in);
int x = 0;
while(x < 1) {
String text = input.nextLine();
text = text.toUpperCase();
int punctuations = 0;
int vowels = 0;
int consonants = 0;
System.out.println("Your text: " + text.toLowerCase());
for (int i = 0; i < text.length(); i++) {
char character = text.charAt(i);
if (character == '!' || character == '?' || character == ',' || character == '.' || character == '-' || character == ';' || character == ':' || character == ' ') {
++x;
punctuations++;
}
else if (character == 'A' || character == 'E' || character == 'I' || character == 'Y' || character == 'O' || character == 'U') {
++x;
vowels++;
}
else if (character >= (char)97 || character <= (char)122){
++x;
consonants++;
}
else {
System.out.println("Invalid, try again");
}
}
System.out.println("Punctuations: " + punctuations);
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
}
}
Aucun commentaire:
Enregistrer un commentaire