Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: (Real beginner) Removing space before the first word when switched

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default (Real beginner) Removing space before the first word when switched

    This one little problem has been bugging me for 1 whole hour.
    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?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    skw4712 (July 13th, 2011)

Similar Threads

  1. Mapping real coordinates into GUI
    By gloor in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2011, 08:35 AM
  2. Testing if dates are real or not.
    By cardsfan33 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2011, 07:46 PM
  3. removing stopwords from a word list
    By jessie in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 15th, 2010, 11:02 AM
  4. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 08:10 AM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM