Results 1 to 4 of 4
  1. #1
    DoubleAG is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default How to add spaces

    I am writing a program that writes 100 integers created randomly. I need spaces after each integer but I can't figure out how to do that. Here's the code that I have. Thanks for the help.

    for (int i = 0; i < 100; i++)
    output.format("%d", (int)(Math.random() * 100000));

  2. #2
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    I would suggest converting the Integer to a string before adding it add adding a space to the string. are you writing the values to a file?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    To add spaces in a loop, you would do something like this
    Java Code:
    for(int i = 0; i < x; ++i){
      System.out.println("Hello "); //notice the space after the o
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Is output a String? If so you are using the String format() method. It is worth following the link given in the API docs that describe the format string argument. ("%d").

    The format string can specify both the width of the resulting string and the alignment which in your case would be "left".

    The format() method is a static one, so it generally used as String.format(). There are similar methods in other classes that use the same format string syntax. For example output might be a Formatter instance obtained from a StringBuilder or similar. In that case you use it as output.format() as in your example. The first (format) argument would be as below.

    Java Code:
    public class Test {
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                String output = String.format("%-5d|", (int)(Math.random() * 100000)); 
                System.out.print(output);
            }
        }
    }

    The format string "%-5d" prints the argument as a decimal integer, at least 5 wide and is left aligned (-).
    Last edited by pbrockway2; 05-01-2011 at 01:54 AM.

Similar Threads

  1. deleting spaces !!
    By young developer in forum New To Java
    Replies: 7
    Last Post: 03-30-2011, 04:11 PM
  2. problem with spaces in
    By olli_m in forum IntelliJ IDEA
    Replies: 0
    Last Post: 11-24-2009, 06:55 PM
  3. How do i add spaces ~(very simple)
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 11-02-2008, 03:01 AM
  4. Load URL that contains spaces?
    By barkster in forum Java Applets
    Replies: 0
    Last Post: 01-30-2008, 10:40 PM
  5. Help with a word, if it is divided by spaces
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:31 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
  •