(Real beginner) Removing space before the first word when switched
This one little problem has been bugging me for 1 whole hour.
Code Java:
import java.util.Scanner;
public class Project8 {
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
String s1, s2;
System.out.println("Enter a line of text. No punctuation please.");
s1 = keyboard.next();
s2 = keyboard.nextLine();
System.out.println("I have rephrased that line to read:");
System.out.println("" + s2 + " " + s1);
}
}
If I run this, I get:
-Enter a line of text. No punctuation please.
-What the hell
-I have rephrased that line to read:
- the hell What
I want the space in the rephrased line before "the" gone...How IS IT POSSIBLE TO REMOVE?
Re: (Real beginner) Removing space before the first word when switched
Hello and welcome to the Java Programming Forums.
This can be easily fixed by updating:
System.out.println("" + s2 + " " + s1);
to
System.out.println(s2.trim() + " " + s1);
the .trim() method removes the white space.