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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started