Hey there fellow programmers,
I'm having a little trouble with this program of mine, it's supposed to read in a line of text, then output a line of text so that the first word and the last word are swapped. The only problem is when I try to find the first position in the line after using the trim() method, it sets the index as 0 and doesn't display the firstWord. If someone could gracefully fix & explain my mistake, I would gladly appreciate it. By the way, I am new to programming.
public static void main(String[] args) {
//Text
String txt = " one two three four five";
//use trim method to remove spaces before the firstWord and after lastWord
String firstWord = txt.trim();
String lastWord = txt.trim();
//use indexOf to find the first space position in the line
int index = txt.indexOf(' ');
//use substring to get firstWord and the rest of the line
firstWord = txt.substring(0, index);
//use lastIndexOf to find the last space position in the line
int lastIndex = txt.lastIndexOf(' ');
//use substring to find the last word
lastWord = txt.substring(lastIndex);
//form a new string using concatenation
System.out.println(lastWord " " firstWord);
}
CodePudding user response:
I got your mistake. You were actually trimming the and storing in other variables and using the variable txt in your other code. So you need to trim txt and store in txt itself (or maybe you can store in any other variable but then you will have to use that variable only). Also initializing firstWord and lastWord with trimmed value doesn't make sense since you don't use that value and again initialize it.
public static void main(String[] args) {
//Text
String txt = " one two three four five";
//use trim method to remove spaces before the firstWord and after lastWord
txt = txt.trim();
String firstWord, lastWord;
//use indexOf to find the first space position in the line
int index = txt.indexOf(' ');
//use substring to get firstWord and the rest of the line
firstWord = txt.substring(0, index);
//use lastIndexOf to find the last space position in the line
int lastIndex = txt.lastIndexOf(' ');
//use substring to find the last word
lastWord = txt.substring(lastIndex);
//form a new string using concatenation
System.out.println(lastWord " " firstWord);
}
CodePudding user response:
Welcome to the Stackoverflow!
Since your goal is swapping only the first and last words of the line, this could be also achieved with a little help of arrays. Utilizing an array might simplify your code a bit.
If you're trying to achieve this without using arrays, the answer above should help. Otherwise, I would go with something like this:
public static void main(String[] args) {
String txt = " one two three four five";
// use trim method to remove spaces before and after, then split the line into words
String[] words = txt.trim().split(" ");
// Get the first word
var firstWord = words[0];
// Replace the first word with last one
words[0] = words[words.length-1];
// Put the first word to the end
words[words.length-1] = firstWord;
System.out.println(Arrays.toString(words));
}
CodePudding user response:
@Utkarsh Sahu 's answer works fine, but have you considered using split?
String txt = "one two three four five";
String split[];
split = txt.split("\\s ");
System.out.println(split[split.length-1] " " split[0]);
I think it's an easier approach.
CodePudding user response:
When you write
//use trim method to remove spaces before the firstWord and after lastWord
String firstWord = txt.trim();
String lastWord = txt.trim();
you store the trimmed text in variables that you don't use.
Replace these 2 lines with txt = txt.trim(); And you should get the desired result.
