Bookmark Topic Watch Topic
  • New Topic

Need help to move First word to the last.  RSS feed

 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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:

 
Deepak Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello,

Please let me know what is the desired output. Is it "is the language Java"???

The issue with the code here is the line:

int last = s.indexOf("");

This returns 0. This is because the output of the line

String s = kb.next(); is Java and this does not contain any "".

Hence, when you do a substring from 0 to 0, you do not get anything in the output.

Hope this helps.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question more than once.

I'm locking this one; please continue in the other copy.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!