Answer to Question #305727 in Java | JSP | JSF for gunna_b

Question #305727

Using the provided recording and your knowledge from programming 1. 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. Sample Run1 Enter a number or word: 7 Output1: 7 to the power 3 = 343 Sample Run2 Enter a number or word: NAMIBIA Output2: The word split in half is : NAMI and BIA


1
Expert's answer
2022-03-03T19:07:48-0500
package com.task;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number or word:");
        String input = sc.next();
        try {
            Integer inputInt = Integer.valueOf(input);
            System.out.println(inputInt + " to the power 3 = ");
            System.out.println(inputInt*3);

        } catch (NumberFormatException e) {
            int halfLength = input.length()/2;
            String first = input.substring(0, halfLength);
            String second = input.substring(halfLength);

            System.out.println("The word split in half is: " + first + " and " + second);

        }
    }

}

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