As a build up from the previous question, consider 3 possible scenario
of input : A character, number or word. You program should the do the following: In case
the input is a character it should indicate if it’s a vowel or not, when a number is entered
then it’s should check if it’s a prime and finally if a word is entered the system should check
if it’s a palindrome or not
import java.math.*;
import java.io.*;
import java.util.*;
import java.lang.StringBuilder;
public class MyClass {
public static void main(String args[]) {
if(args[0].matches("[aeiouy]")){
System.out.println("it’s a vowel");
}else if(args[0].matches("\\d+")){
int number = Integer.parseInt(args[0]);
BigInteger bigInteger = BigInteger.valueOf(number);
boolean probablePrime = bigInteger.isProbablePrime((int) Math.log(number));
if(probablePrime){
System.out.println("it’s a prime");
}else{
System.out.println("it’s not a prime");
}
}else if(args[0].length() > 1){
int boolPalindrome = args[0].compareToIgnoreCase(new StringBuilder(args[0]).reverse().toString());
if(boolPalindrome == 0){
System.out.println("It's a palindrome");
}else{
System.out.println("It's not a palindrome");
}
}else{
System.out.println("it’s not a vowel");
}
}
}
Comments
Leave a comment