- #1
azolotor
- 9
- 0
Write a program that will read a line of text as input and then display the line with the first word moved to the end of the line. For example, a possible sample interaction with the user might be.
"Enter a line of text. No punctuation please.
java is the language
I have rephrased that line to read:
Is the language Java"
Assume that there is no space before the first word and that the end of the first word is indicated by a blank, not by a comma or other punctuation. Note that the new first word must begin with a capital letter.
Attempt:
import java.util.Scanner;
public class stringManipulator {
public static void main(String[] args) {
Scanner get = new Scanner (System.in);
String userInput;
System.out.println("Enter a line of text. No punctuation please.");
userInput = get.nextLine();
List<String> order =
new ArrayList<String>(Arrays.asorder(userInput.split("\\userInput+")));
order.add(order.size() - 1, order.remove(0));
StringBuilder newOrder = new StringBuilder();
for( String word : order){
if(newOrder.length() > 0){
newOrder.append(' ');
}
newOrder.append(word);
}
System.out.println("I have rephrased that line to read:");
System.out.println(newOrder.toString());
}
}
I first tried to find a method in the String class that would replace the first word with the final and then use the method to capitalize a character at a specific position. However, i was unable to find any such method. I find this solution on a Java forum that creates an array and then uses some list method that I am still trying to comprehend and cannot get to work. I thought of breaking the string up into substrings and rearranging it that way but I cannot think of a method that would do that without already knowing the input. Since it is based on user input there is no way to predict what they will enter so I am very confused.
"Enter a line of text. No punctuation please.
java is the language
I have rephrased that line to read:
Is the language Java"
Assume that there is no space before the first word and that the end of the first word is indicated by a blank, not by a comma or other punctuation. Note that the new first word must begin with a capital letter.
Attempt:
import java.util.Scanner;
public class stringManipulator {
public static void main(String[] args) {
Scanner get = new Scanner (System.in);
String userInput;
System.out.println("Enter a line of text. No punctuation please.");
userInput = get.nextLine();
List<String> order =
new ArrayList<String>(Arrays.asorder(userInput.split("\\userInput+")));
order.add(order.size() - 1, order.remove(0));
StringBuilder newOrder = new StringBuilder();
for( String word : order){
if(newOrder.length() > 0){
newOrder.append(' ');
}
newOrder.append(word);
}
System.out.println("I have rephrased that line to read:");
System.out.println(newOrder.toString());
}
}
I first tried to find a method in the String class that would replace the first word with the final and then use the method to capitalize a character at a specific position. However, i was unable to find any such method. I find this solution on a Java forum that creates an array and then uses some list method that I am still trying to comprehend and cannot get to work. I thought of breaking the string up into substrings and rearranging it that way but I cannot think of a method that would do that without already knowing the input. Since it is based on user input there is no way to predict what they will enter so I am very confused.