# Java-String manipulation (first word to last)

• Comp Sci
azolotor
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+")));
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.

Homework Helper
Gold Member
If you haven't learned about the List<T> class, then you might just try using an array of strings produced by the string.split() method. Something along the lines of the following:

Code:
// split string at each space
String[] strArray = userInput.split(\\s);
String userOutput;
for(int i=1; i< strArray.length; i++)
{
// write code here to append ith element of array to userOutput, separated by a space
}
//write code here to move uncapatalize 0th element of array and move append it to the end of userOutput, separad by a space
// write code to capitalize first letter of first word of userOutput

azolotor
We haven't learned about arrays yet either so I am unsure of how to set one up exactly. I apologize for making this more difficult but could you break down your explanation a little bit more for me. I am having trouble understanding

Homework Helper
Gold Member
We haven't learned about arrays yet either so I am unsure of how to set one up exactly. I apologize for making this more difficult but could you break down your explanation a little bit more for me. I am having trouble understanding

Learning about the string.split method before learning basic properties of an array = craziness.

The statement String[] strArray (notice the square brackets) defines an array of Strings, with an unspecified number of elements. The statement String[] strArray = userInput.split("\\s") defines and array with an unspecified number of elements, and then assigns the result of the string.split() call to it. The statement userInput.split("\\s") splits the userInput string into substrings at each occurrence of a whitespace character (the regular expression for a space is \s, the additional \ is an escape character necessary because of the double quotes) and returns those substring (in order) in an array. The number of elements in that array will equal the number of substrings that were separeated by a space. The number of elements of an array is given by the array.length attribute. The "first" element in an array is array[0], so if you define String[] strArray = userInput.split("\\s"), then strArray[0] will be the first word (as a String) in the userInput string; strArray[1] will be the second word (if there is one!), and so on up until strArray[strArray.length], which will be the last word.

In your case, it is probably best to use the limit parameter of the string.split() method so that you only get 2 elements in your array: String[] strArray = userInput.split("\\s", 2) will split userInput into two pieces; the first piece will be everything up untill the first whitespace (i.e. the first word) and will be assigned to strArray[0]. As long as there is a whitespace character, and something after it, everything after it will be assigned to strArray[1]. If there is only one word in userInput, then there will be no second element strArray[1], so before referring to it, you should test to see if it exists by looking at strArray.length (the number of elements in the array).