Add column in java


I'm trying to do an exercise where the user must enter the number of columns and the character he wants to appear. I'm a little messed up when it comes to doing it. I leave the statement:

Make a program using loops that shows the following figure on the screen, asking the user in advance how many rows you want to display and which character you want to use (for example, *). You must perform exception handling.


public class ejercicio3 {

    public static void main(String[] args) {
    Scanner teclado;
    int Ncolumnas;
    String caracter;

    System.out.print("Introduce cuantas filas deseas mostrar: ");
            teclado = new Scanner(;
            Ncolumnas = teclado.nextInt();
    System.out.print("Introduce un caracter: ");
            teclado = new Scanner(;
            caracter =;

            for(int i=1;i>Ncolumnas; i++){
            if (Ncolumnas>0){

            System.out.print("Introduce un caracter: " + (caracter + Ncolumnas));

            System.out.print("Error ");
    }catch(Exception e){
    System.out.println("Error, no has introducido un carácter");



When I do this, the result does not appear, because obviously, there is something I am doing wrong.

PS: Rookie in sight.

asked by Flowcou 15.12.2018 в 13:06

1 answer


Then at first glance you can see the error:

for (int i = 1; i > Ncolumnas; i++) { // inicias i=1
  i++;  // aquí ya i = 2
  if (Ncolumnas > 0) { // nunca vas a entrar

An example to iterate through Ncolumnas is:

for (int i = 0; i < Ncolumnas; i++) {
   System.out.println(i); // code

In this example you have executed the line commented by "code" Ncolumnas times

answered by 15.12.2018 в 15:20