Malintha
Malintha

Reputation: 4776

Getting additional empty line

When I run this code I am getting additional line after I enter the the first number and before this prints staircase.

6


     #
    ##
   ###
  ####
 #####
######

You can see the additional line between 6 and the staircase

import java.util.*;

public class Solution {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    System.out.println("");
    for(int i=0;i<=n;i++){
        System.out.print(new String(new char[n-i]).replace("\0", " "));
        System.out.print(new String(new char[i]).replace("\0", "#"));
        System.out.println("");
    }
}
}

Upvotes: 1

Views: 42

Answers (2)

Guy
Guy

Reputation: 51009

In the first iteration you are printing n blanks and zero #.

Initializing i = 1 in the loop will print only n-1 blanks before printing one #.

Upvotes: 4

EvilTak
EvilTak

Reputation: 7579

In addition to @guy's answer, just initialize i to 1 and that should fix your problem.

for(int i=1; i<=n; i++)

Upvotes: 1

Related Questions