Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help to move First word to the last.

 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags:



Can you please clarify what logic are you using to interchange the first and last words of a sentence?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell Ritchie ,

Thank you so much for your words. Will try to do the same.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic