Java-String manipulation (first word to last)

  • Comp Sci
  • Thread starter azolotor
  • Start date
  • Tags
    Manipulation
In summary: The line of code for(int i=1; i< strArray.length; i++) { /* loop body */ } sets the loop to run from i=1 (since strArray[0] has already been "moved" to the end of the string) to i=strArray.length, which is the index of the last element in the array. You want to keep adding each element to the end of the string userOutput, so you will need to use the string concatenation operator +. Something like the following loop body might work:if (i>1) { userOutput = userOutput + " "; // add a space to separate words }userOutput = userOutput + strArray[i];This appends the ith element of
  • #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.
 
Physics news on Phys.org
  • #2
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
 
  • #3
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
 
  • #4
azolotor said:
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 until 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).
 
  • #5


I would suggest using the split() method to divide the user input into an array of strings. Then, you can use the first word as the pivot and move it to the end of the array using the add() and remove() methods. Finally, use the StringBuilder class to concatenate the strings in the array and display the new sentence. This approach is more efficient and flexible than trying to manipulate the input string directly. Additionally, I would suggest adding error handling to account for any unexpected inputs from the user.
 

1. What is Java-String manipulation?

Java-String manipulation is the process of modifying or manipulating strings in Java programming language. It involves various methods and techniques to change the content or structure of a string.

2. How do I extract the first word from a string in Java?

To extract the first word from a string in Java, you can use the split method and specify a space " " as the delimiter. This will split the string into an array of words, and you can access the first word using the index 0.

3. Can I change the case of a string in Java?

Yes, you can change the case of a string in Java using the toUpperCase() and toLowerCase() methods. These methods will return a new string with the desired case, without modifying the original string.

4. How can I replace a specific word in a string with another word?

To replace a specific word in a string with another word, you can use the replace method. This method takes two parameters - the word to be replaced and the word to replace it with. It will return a new string with the replacement done.

5. Is there a way to check if a string contains a certain word or phrase in Java?

Yes, you can check if a string contains a certain word or phrase in Java using the contains method. This method returns a boolean value - true if the string contains the specified word or phrase, and false if it does not.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
937
  • Programming and Computer Science
Replies
3
Views
772
  • Engineering and Comp Sci Homework Help
Replies
10
Views
10K
Back
Top