Control Structures: As a build-up from the previous question, consider 3 possible scenarios of input: A character, number, or word. Your program should then do the following:
import java.math.*;
import java.io.*;
import java.util.*;
import java.lang.StringBuilder;
public class ControlStructures{
public static void main(String args[]) {
// checking for vowels
if(args[0].matches("[aeiouy]")){
System.out.println("it’s a vowel");
// checking for number
}else if(args[0].matches("\\d+")){
int number = Integer.parseInt(args[0]);
BigInteger bigInteger = BigInteger.valueOf(number);
// checking for prime 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");
}
// checking for word
}else if(args[0].length() > 1){
// reverse word then compare without upper case
int boolPalindrome = args[0].compareToIgnoreCase(new StringBuilder(args[0]).reverse().toString());
// checking for palindrome from result
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