Results 1 to 4 of 4
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default How do i add spaces ~(very simple)

    How do i add spaces to the line in bold, thought +'s would of done this, please help?



    public class travel{

    public static void main (String[]arg) {

    double speed =85;
    double time;
    double distance;

    System.out.println("Please enter the distance in Kilometres");
    distance=UserInput.readDouble();

    time=distance/speed;


    int hours;
    int minutes;
    int seconds;

    hours= (int)(time/3600);
    minutes=(int)(time/60);


    System.out.println("To travel"+ distance +"kilometres it takes"+ hours +"hours"+ minutes +"minutes and"+ (int)time +"seconds");



    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    "To travel"
    "To travel "
    Put the spaces inside the ""s

    A space is just like any other of the 120+ characters you can enter from the keyboard. Putting them inside a pair of ""s makes them part of the String.
    Last edited by Norm; 11-01-2008 at 10:08 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    no the pluses just concatenate, add one string right to the other. You wouldn't want them to add spaces as you loose all flexibility that way (how would you concatenate without spaces?). To add spaces, simply add them:
    Java Code:
    System.out.println("To travel " + distance + " kilometres it takes " + hours + " hours " + minutes + " minutes and " + (int)time + " seconds");

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I like the way Fubarable explain in the above post.

    Some people use separate variable for that too, so he/she is going to use it through the complete application.

    Java Code:
    String strSpace = " ";
    System.out.println("To travel" + strSpace + distance + strSpace + "kilometers it take"); // Continue like that
    But for me it is ugly to use. ;)

Similar Threads

  1. Caesar and encoding with block spaces
    By Franneldort in forum New To Java
    Replies: 13
    Last Post: 10-30-2008, 04:48 PM
  2. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  3. Load URL that contains spaces?
    By barkster in forum Java Applets
    Replies: 0
    Last Post: 01-30-2008, 09:40 PM
  4. Help with a word, if it is divided by spaces
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:31 AM
  5. Reading file data that contains no spaces
    By jdepue in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •