Answer to Question #301306 in Java | JSP | JSF for Caser

Question #301306

import java.util.Scanner;


public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter a String");

String line = in.nextLine();

while (true) {

int space = line.indexOf(' ');

String word = line.substring(0, space == -1 ? line.length() : space);

System.out.println(word + " " + word.length() + " " + (word.length() > 1 ? word.substring(word.length() - 1)

+ word.substring(1, word.length() - 1) + word.substring(0, 1) : word));

line = line.substring(space + 1);

if (space == -1) {

break;

}

}

}

}

Do the coding so that output will remain same but ternary operator will not be used.


1
Expert's answer
2022-02-22T15:14:07-0500
import java.util.Scanner;


public class App {


	public static void main(String[] args) {


		Scanner in = new Scanner(System.in);
		System.out.println("Enter a String");
		String line = in.nextLine();


		while (true) {
			int space = line.indexOf(' ');
			String word = line.substring(0, line.length());
			if (space != -1) {
				word = line.substring(0, space);
			}
			if (word.length() > 1) {
				System.out.println(word + " " + word.length() + " " + (word.substring(word.length() - 1)
						+ word.substring(1, word.length() - 1) + word.substring(0, 1)));
			} else {
				System.out.println(word + " " + word.length() + " " + word);
			}
			line = line.substring(space + 1);
			if (space == -1) {
				break;
			}
		}
		in.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