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 4 of 4

Thread: Cant get spaces

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cant get spaces

    This is my code, but when i compile it there are no spaces between the time and the word AT. Thanks

    {
    	public static void main (String[] args)
    	{
    		int batspeed = 121;
    		int jokerspeed = 110;
    		int batdistance;
    		int jokerdistance;
     
    		batdistance = 0;
    		jokerdistance = 110;
    		System.out.printf ("At 12:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 1:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 2:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 3:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 4:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 5:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 6:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 7:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 8:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 9:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 10:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
    		batdistance = batdistance+ batspeed;
    		jokerdistance = jokerdistance + jokerspeed;
    		System.out.printf ("At 11:00\n");
    		System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance);
     
     
    	}
    }
    Last edited by helloworld922; October 13th, 2010 at 12:51 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Cant get spaces

    Are you saying that this statement:
    System.out.printf ("At 12:00\n");
    prints:
    At12:00
    instead of:
    At 12:00
    If so, I have no idea wtf is happening. Very odd. I guess my suggestion would be to use a println instead of printf and the new line character. So instead of this:
    System.out.printf ("At 12:00\n");
    say:
    System.out.println ("At 12:00");
    See if that works.

    I would say you came from C based on your print outs. To get you assimilated with JAVA, I will give you a little bit of info about printing to the console.

    There are 3 types of print statements (that I know of).
    1) System.out.printf();
    This is a very rare thing to see in JAVA, as it is only important when you are trying to format your output particular ways (decimal points and what not). I won't go into detail for this one, as it works the same way as it does in C, and you obviously already know it.
    2) System.out.print();
    This is a more common printing method that will be found in JAVA. You can do the same thing as in printf statements, except you can directly concatenate Strings, instead of having placeholders like with the printf statements. You can concatenate Strings by using the plus (+) symbol. Essentially, System.out.printf ("Batman distance is %d, Joker distance is %d", batdistance ,jokerdistance) == System.out.print("Batman distance is "+batdistance+", Joker distance is "+jokerdistance)
    3) System.out.println();
    This is the most common printing method that you will find in JAVA. This statement prints the indicated String or Object to the console. After that has been printed, the cursor will move to the next line. Essentially, System.out.println() == System.out.print("\n")


    Tell me if that all makes sense. If you have any questions, I'll be happy to answer them.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Cant get spaces

    Quote Originally Posted by aussiemcgr View Post
    Are you saying that this statement:
    System.out.printf ("At 12:00\n");
    prints:
    At12:00
    instead of:
    At 12:00
    If so, I have no idea wtf is happening. Very odd.
    That's because that's not happening.

    What is happening is that the second of each pair of printf statements lacks a newline, resulting in the next "At" being tagged on the end of the same line, without so much as a space.

    @KSmart(?)man: If you can't describe your problem correctly, you're not likely to get useful help.

    db

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Cant get spaces

    Quote Originally Posted by Darryl.Burke View Post
    That's because that's not happening.

    What is happening is that the second of each pair of printf statements lacks a newline, resulting in the next "At" being tagged on the end of the same line, without so much as a space.

    @KSmart(?)man: If you can't describe your problem correctly, you're not likely to get useful help.

    db
    I thought that was probably it. So I figured I would give the all the printing information. Thats good stuff to know.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How to remove the in-between spaces in a String? Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 2
    Last Post: February 4th, 2010, 04:19 PM
  2. [SOLVED] How to Put Spaces in the Output of Passed Arguments?
    By EmSaint in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 04:04 PM