Answer to Question #308089 in Java | JSP | JSF for Chakalaka

Question #308089

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 Run 1

Enter a number or word: 7

Output 1:

7 to the power 3= 343

Sample Run 2

Enter a number or word: NAMIBIA

Output 2:

The word split in half is : NAMI and BIA 


1
Expert's answer
2022-03-10T07:15:27-0500


import java.util.*;


class App {


	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter a number or word: ");
		String input = keyboard.next();
		try {
			Integer inputInt = Integer.valueOf(input);
			System.out.println(inputInt + " to the power 3 = " + (inputInt * 3));
		} catch (NumberFormatException e) {
			int middle = input.length() / 2;
			if (input.length() % 2 != 0) {
				middle = (input.length() + 1) / 2;
			}


			String firstPart = input.substring(0, middle);
			String secondPart = input.substring(middle);
			System.out.println("The word split in half is: " + firstPart + " and " + secondPart);
		}
		keyboard.close();
	}
}

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