Java-String manipulation (first word to last)

  • Context: Comp Sci 
  • Thread starter Thread starter azolotor
  • Start date Start date
  • Tags Tags
    Manipulation
Click For Summary

Discussion Overview

The discussion revolves around a programming task in Java that involves manipulating a string by moving the first word of a user-inputted line of text to the end of that line. Participants explore various methods to achieve this, including the use of arrays and string manipulation techniques. The conversation includes technical explanations, code snippets, and requests for clarification.

Discussion Character

  • Technical explanation
  • Homework-related
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant shares an initial code attempt but expresses confusion about using the List class and string manipulation methods.
  • Another participant suggests using an array of strings produced by the string.split() method as an alternative to the List class.
  • There is a request for a breakdown of how to set up arrays, indicating a lack of familiarity with the concept among some participants.
  • A later post elaborates on the string.split() method, explaining how it can be used to split a string into an array based on whitespace and how to access elements of that array.
  • Participants discuss the importance of understanding arrays before using them in conjunction with string manipulation methods.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding arrays and string manipulation. While some provide technical explanations, others indicate confusion and request further clarification. There is no consensus on the best approach, as participants are exploring different methods and expressing uncertainty.

Contextual Notes

Some participants mention not having learned about arrays yet, which may limit their ability to fully engage with the proposed solutions. There are also references to specific methods and syntax that may require prior knowledge of Java programming concepts.

azolotor
Messages
9
Reaction score
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
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
 
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
 
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).
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
11K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K