. Create a program in java that takes in an input from the user and checks whether it’s a number or a word. In case a number is entered the program should print the number to the power 3 and if a word is entered your program should split that word into 2.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
try {
int number = Integer.parseInt(line);
System.out.println((int) Math.pow(number, 3));
} catch (NumberFormatException e) {
if (line.length() > 1) {
System.out.println(line.substring(0, line.length() / 2) + " " + line.substring(line.length() / 2));
} else {
System.out.println(line);
}
}
}
}
Comments
Leave a comment