How to send a received variable in the main method to another class to be treated

Asked

Viewed 52 times

0

Code:

public class Matrix {

    public static void main(String[] args) {
        Menu();
    }

    private static void Menu() {
       CreateMatrix creatematrix = new CreateMatrix();
        int escalar = 0;
        int option = 0;
        Scanner op = new Scanner(System.in);
        do{                    System.out.println("       \n\n##################### Matrix Calculator –  ###################");
            System.out.println("\n                  ==============================================");
            System.out.println("                  |     1 - Add two matrices                     |");
            System.out.println("                  |     2 - Subtract Matrices                    |");
            System.out.println("                  |     3 - Scalar Multiplication of a Matrix    |");
            System.out.println("                  |     4 - Multiply two matrices                |");
            System.out.println("                  |     0 - QUIT PROGRAM                         |");
            System.out.println("                  ==============================================\n");
            System.out.print("\n");                        
                        option = op.nextInt();

                  switch(option){
        case 0:         
                        System.out.println("User Finished Program");
            break;
        case 1:

                        break;

        case 2:
                        System.out.println("Subtract Matrices");
                        chamarMatrizdivisao();
            break;
                case 3:
                        System.out.println("Scalar Multiplication of a Matrix");
            System.out.println("Digite o escalar: ");
                        escalar = op.nextInt();
                        break;

            case 4:
            System.out.println("Multiply two matrices");
                        chamarMatrizMultiplicacao();
            break;
        default:
            System.out.println("Invalid option!!!");
        }
        }while (option !=0);
            System.out.println("Invalid value ou Program is being closed");
        }
    public static void chamarMatrizMultiplicacao(){
        Conteudo oConteudo = new Conteudo();
         List<String> lista = oConteudo.Matriz();
         oConteudo.MultiplicacaoMatriz22(lista);
    }
    public static void chamarMatrizdivisao(){
        Conteudo oConteudo = new Conteudo();
         List<String> lista = oConteudo.Matriz();
         oConteudo.DivisaoMatriz22(lista);
    }
}

I am asking the user to type the scalar and I want to take this variable to my class and deal with the method that follows below:

    public void EscalarMatriz22(List<String> conteudo){
       Matrix mtx = new Matrix();
       int[] NumerosMatrizesEscalar = new int[conteudo.size()];

       for (int ndxv = 0; ndxv < conteudo.size(); ndxv++) {
       try{
       int numv = Integer.parseInt(conteudo.get(ndxv));
       NumerosMatrizesEscalar[ndxv] = Integer.parseInt(conteudo.get(ndxv));
       } catch(Exception eox){
        }
        }
       int div11 = NumerosMatrizesEscalar[2] - NumerosMatrizesEscalar[9]; 

       int div12 = NumerosMatrizesEscalar[4] - NumerosMatrizesEscalar[10]; 

        int div21 = NumerosMatrizesEscalar[3] - NumerosMatrizesEscalar[11];  

        int div22 = NumerosMatrizesEscalar[5] - NumerosMatrizesEscalar[12]; 

        System.out.println(div11);
        System.out.println(div12);
        System.out.println(div21);
        System.out.println(div22);

   }

1 answer

1


Hello, all right?

I’m not sure I understand what you mean, but you can create a function inside the class, but outside the main, where you will receive the option read in the main by parameter, will do the treatment you need and will return true in case you have according to what you need or false if you do not agree.

Put a boolean variable receiving the return of this function.

For example:

boolean resultado = nomeDaFuncao(int parametro);

Once you have the result at hand, you can create the conditions any way you want, for example an error message.

Please, if I’ve run too far from your doubt, answer me your question so I can help you!

  • What I want is to get what the user typed in this part of the code case 3:&#xA; System.out.println("Scalar Multiplication of a Matrix");&#xA; System.out.println("Digite o escalar: ");&#xA; escalar = op.nextInt();&#xA; break; this scalar variable I want to take to the other class of my method public class Conteudo {public void EscalarMatriz22(List<String> conteudo){&#xA;

  • Your idea is to call this function after the value is read? You could modify the function to pass one more parameter, for example: public void Scalarmatriz22(List<String> content, int scalar) That would be the question?

  • That’s right, however I’m not able to do it, always gives error in when trying to add int scalar.

  • Are you changing both the creation part of Function, and the part where you call it? If possible attach the error that returns you, maybe I can help you.

Browser other questions tagged

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