Question: In Java I need to create a simple program that reverses any line of a String using .split method and StringBuilder. We are instructed to
In Java I need to create a simple program that reverses any line of a String using .split method and StringBuilder. We are instructed to use a while loop and was given the hint that it should be done in only 5 lines of code. I posted below what I have, I can't figure out how to implement a while loop using the stringbuilders reverse method to then print out the correct output.
import java.util.Scanner;
public class ReverseAnything{
public static void main(String[] args) {
// Setup a scanner object, and receive user's input
Scanner in = new Scanner(System.in);
System.out.print("Provide an input sentence: ");
String userInput = in.nextLine();
// Step 1: use a split method on the String userInput.
// The argument the split method should be the delimeter " "
//
// Step 1 can be completed in a single line of code.
userInput.split(" ");
System.out.print("The output sentence is : ");
StringBuilder sb = new StringBuilder(userInput);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
