How to insert the size of an array as per user input?

Asked

Viewed 1,768 times

2

How do I enter, asking the user through the class Scanner, the size of the matrix and it tells me the column and row numbers you want, and then print the values, without using methods? I tried but it’s making a mistake:

import java.util.Scanner;

public class Matriz {
    int matriz[][], linha, coluna;

    public Matriz(int linha, int coluna){
        matriz = new int [linha][coluna];
        this.linha = linha;
        this.coluna = coluna;
    }

    public void Inserir(){
        Scanner entrada = new Scanner(System.in);
        for(int x=0; x < linha; x++){
            for(int y=0; y < coluna; y++){
                System.out.println("matriz ["+x+"]["+y+"] =");
                matriz[x][y]= entrada.nextInt();
            }//fim for
        }//fim for
    }//fim inserir

    public void Imprimir(){
        for(int x=0; x < linha; x++){

            for(int y=0; y < coluna; y++){
                System.out.print(matriz[x][y]+"\t");
            }//fim for
            System.out.println();//apenas para quebrar linha
        }//fim for
    }

    public static void main(String [] args){
        Scanner entrada = new Scanner(System.in);
        int linha=0, coluna =0;

        System.out.println("Informe a quantidade de linhas da matriz");
        linha = entrada.nextInt();

        System.out.println("Informe a quantidade de colunas da matriz");
        coluna = entrada.nextInt();
        Matriz mat = new Matriz(linha, coluna);

        mat.Inserir();
        mat.Imprimir();
    }
}

Method-free.

  • What mistake? Try to improve your question?

  • I can’t type numbers.

  • Do you want the user to enter the size of the matrix, and then start it with the given size? Edit the question, it is not at all clear the problem.

  • Yes, as in http://pastebin.com/vp5sFiXC

1 answer

3


After instantiating an array, you can no longer change its size. If you need to set this size after user data entry, just initialize the array, then instantiate the required data entries:

public static void main (String[] args) {
        int matriz[][];

        Scanner entrada = new Scanner(System.in);
        int linha=0, coluna =0;

        System.out.println("Informe a quantidade de linhas da matriz");
        linha = entrada.nextInt();

        System.out.println("Informe a quantidade de colunas da matriz");
        coluna = entrada.nextInt();

        matriz = new int[linha][coluna];

         for(int x=0; x < linha; x++){
            for(int y=0; y < coluna; y++){
                System.out.println("matriz ["+x+"]["+y+"] =");
                matriz[x][y]= entrada.nextInt();
            }
        }

        for(int x=0; x < linha; x++){

            for(int y=0; y < coluna; y++){
                System.out.print(matriz[x][y]+"\t");
            }
            System.out.println();
        }

    }

See working on IDEONE

This way, the array will be created according to the total of rows/columns informed via data entry.

  • Yes, just like that!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.