Answer to Question #304431 in Java | JSP | JSF for Nguu

Question #304431

. 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.


1
Expert's answer
2022-03-02T01:17:23-0500
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);
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS