0

I'm trying to get a single line to output and look somewhat like this:

1 2  3   4    5     6      7       8        9 

Adding another space every time the number increases. I need to do it using for loops, with a nested for loop being preferred. Here's my code so far (on run it doesn't print even with the method call.)

public static void outputNine()
{
    for(int x=1; x<=9; x++)
    {
        for(char space= ' '; space<=9; space++)
        {
            System.out.print(x + space);
        }
    }
}

I know I'm doing something wrong, but I'm fairly new to java so I'm not quite sure what. Thanks for any help.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
DPabst
  • 35
  • 1
  • 7
  • 3
    `for(char space= ' '; space<=9; space++)` will never execute: `space <= 9` is immediately false, because `' ' == 32`. – Andy Turner Feb 16 '17 at 23:43
  • @shmosel I tried your suggestion and received an output but got this "333435363738394041" – DPabst Feb 16 '17 at 23:46

6 Answers6

2

You can initialize space only once, then print the numbers, and for every number, print the spaces:

char space = ' ';
for(int x=1; x<=9; x++)
{
    System.out.print(x);
    for(int i = 0 ; i < x ; i++)
    {
        System.out.print(space);
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
0

Your loop is using the ASCII value of ' ', which isn't what you want. You just need to count up to the current x. Replace your inner loop with this:

System.out.print(x);
for (int s = 0; s < x; s++) {
    System.out.print(" ");
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
0

Right now you're trying to increment a char, which doesn't make sense. You want space to be a number equivalent to the number of spaces you need.

bluecanary
  • 81
  • 5
0

You only need one loop.

Refer: Simple way to repeat a String in java

for (int i = 1; i <= 9; i++) {
    System.out.printf("%d%s", i, new String(new char[i]).replace('\0', ' '));
}

Output

1 2 3 4 5 6 7 8 9

Or more optimally,

int n = 9;
char[] spaces =new char[n];
Arrays.fill(spaces, ' ');
PrintWriter out = new PrintWriter(System.out);

for (int i = 1; i <= n; i++) {
    out.print(i);
    out.write(spaces, 0, i);
}
out.flush();
Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • It'd be better if you built the string once, as big as you need it to be. You can then use the `print(String, int, int)` overload to print part of the string. – Andy Turner Feb 16 '17 at 23:52
  • I meant [`PrintWriter.write(char[], int, int)`](https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#write(char[],%20int,%20int)). Always forget where that is. – Andy Turner Feb 16 '17 at 23:53
  • @AndyTurner Was about to say, that method call doesn't sound familar – OneCricketeer Feb 16 '17 at 23:53
  • 1
    @GrzegorzGórkiewicz not just constructor calls, `replace("\0", " ")` is going to be very slow because it uses regex. `replace('\0', ' ')` would be much better. – Andy Turner Feb 16 '17 at 23:55
  • @AndyTurner Better? – OneCricketeer Feb 17 '17 at 00:02
  • Create all the objects! You create 4 objects when 1 will do. Why not `char[] spaces = new char[n]; Arrays.fill(spaces, ' ');`? – Andy Turner Feb 17 '17 at 00:06
0

Consider the line as composed of 9 parts of the same structure: x-1 spaces followed by x, where x change from 1 to 9.

/*
0 space + "1"
1 space + "2"
2 spaces + "3"
...
*/

int n = 9;
for (int x = 1; x <= n; x++) {
    // Output x - 1 spaces
    for (int i = 0; i < x - 1; i++) System.out.append(' ');
    // Followed by x
    System.out.print(x);
}

One good thing about this approach is you don't have trailing spaces.

xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
0

Please find my simple solution:)

public class Test {

    public static void main(String args[]) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 2; j <= i; j++) {
                System.out.print(" ");
            }
            System.out.print(i);
        }

    }

}