File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need help to move First word to the last. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help to move First word to the last." Watch "Need help to move First word to the last." New topic
Author

Need help to move First word to the last.

Sarah Tod
Greenhorn

Joined: Mar 03, 2013
Posts: 20
Hi All, I am trying to move the first word to the last. But my program did not print the sentence. Please advise me what i am missing. Thank you so much! Here is my program:

enter code here: import java.util.Scanner;

public class FirstLast
{
public static void main(String[] args)
{
System.out.println("Enter line of text. Without punctuation");

Scanner kb = new Scanner(System.in);

String s = kb.next();

int last = s.indexOf("");

s = s.substring(0, last);

System.out.println("I have rephrased that line to read:");

System.out.println(s);

}
}

My output is: Enter line of text. Without punctuation
Java is the language
I have rephrased that line to read:

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Please use code tags:



Can you please clarify what logic are you using to interchange the first and last words of a sentence?


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Apart from the fact that you are not changing anything in that sentence, I recommend you print the index of "" first, and see what that give you.
Balaguru Gupta
Greenhorn

Joined: Mar 03, 2013
Posts: 10
Sb Td wrote:Hi All, I am trying to move the first word to the last. But my program did not print the sentence. Please advise me what i am missing. Thank you so much! Here is my program:

enter code here: import java.util.Scanner;

public class FirstLast
{
public static void main(String[] args)
{
System.out.println("Enter line of text. Without punctuation");

Scanner kb = new Scanner(System.in);

String s = kb.next();

int last = s.indexOf("");

s = s.substring(0, last);

System.out.println("I have rephrased that line to read:");

System.out.println(s);

}
}

My output is: Enter line of text. Without punctuation
Java is the language
I have rephrased that line to read:



Hello mate,

Get the first word and last word by

String[] splittedSentence = sentence.split("\\s+");
String firstWord = splittedSentence[0];
String lastWord = splittedSentence[splittedSentence.length-1];

After obtaining this, just swap the first and last word.

Cheerz mate
Sarah Tod
Greenhorn

Joined: Mar 03, 2013
Posts: 20
Thank you! Let me try. I am trying to move "Java" to "is the language java". If still doesn't work, i'll let you guys kno.
I actually duno what logic i m using. Whatever works for me actually ahaha i m a newbie absolutely 0 knowledge of programming.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Sb Td wrote:Thank you! Let me try. I am trying to move "Java" to "is the language java". If still doesn't work, i'll let you guys kno.
I actually duno what logic i m using. Whatever works for me actually ahaha i m a newbie absolutely 0 knowledge of programming.


Please do not use slang language. It not only makes your query difficult for other people on the forum to understand, but in the long run, it shall spoil your grammar as well.
Sarah Tod
Greenhorn

Joined: Mar 03, 2013
Posts: 20
I will try to use proper grammer from now on. I need to use substring for this exercise, howerver, i am totally clueless about using it. I was told not to use int last = s.substring (0, last) as this results to nothing.
I am so stressed out by this exercise & i still haven't gotten a correct format from anyone. Please advise. Your kind help is greatly appreciated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Balaguru Gupta, welcome to the Ranch

Original Poster: I think you have reached the stage where you need desperate measures. You need to remove a certain piece of hardware which is interfering with your work. It is long thin and black and runs from your computer to the wall.
Now get a sheet of paper and a pencil and an eraser (the latter piece of hardware is the most important). Then you can work out how you would do it on paper. When you have got that written down, without using any computing words at all, you will have some idea what a program might do to execute it.
Balaguru Gupta
Greenhorn

Joined: Mar 03, 2013
Posts: 10


I know this is kinda not an efficient way to do.
Is there any jar available to do the same in efficient and simple way?

Cheerzz
Balaguru Gupta
Greenhorn

Joined: Mar 03, 2013
Posts: 10
Hello Campbell Ritchie ,

Thank you so much for your words. Will try to do the same.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Balaguru Gupta, please read this, which is at the top of this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
You are not actually helping anybody to learn by providing a complete answer like that. Don’t be annoyed with me, but I have pulled rank and removed your answer.
By the way: in the answer I removed, you had a 20‑element String[] array. Why did it have 20 elements?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help to move First word to the last.