How to show all values of three matrices in a single message without using indexes

Asked

Viewed 210 times

0

package AlgoritmosII;

import javax.swing.*;

public class Trabalho1 {

    public static void main(String[] args){
        String[][] A = new String[3][3];
        int[][] B = new int[3][3];
        double[][] C = new double[3][3];
        double[] Ordenar_C = new double[9];
        double recebimento, pagamento, maior_saldo=0, aux;
        int flag=0, numero, i, j, n1=0, n2=0, Control, k=0, troca, fim;
        A[0][0]= "Tibúrcio";
        A[0][1]= "Ricardo";
        A[0][2]= "João";
        A[1][0]= "Felisbino";
        A[1][1]= "Seu Zé";
        A[1][2]= "Dona Maria";
        A[2][0]= "Gedonstrôncio";
        A[2][1]= "Batman";
        A[2][2]= "Homem de Ferro";
        B[0][0]= 111;
        B[0][1]= 222;
        B[0][2]= 333;
        B[1][0]= 444;
        B[1][1]= 555;
        B[1][2]= 666;
        B[2][0]= 777;
        B[2][1]= 888;
        B[2][2]= 999;
        C[0][0]= 0.0;
        C[0][1]= 0.0;
        C[0][2]= 0.0;
        C[1][0]= 0.0;
        C[1][1]= 0.0;
        C[1][2]= 0.0;
        C[2][0]= 0.0;
        C[2][1]= 0.0;
        C[2][2]= 0.0;
        while(flag==0) {
            Control = Integer.parseInt(JOptionPane.showInputDialog(" 1- Inserir Recebimento \n 2- Inserir Pagamento \n 3- Mostrar informações \n 4- Correntista mais rico \n 5- Saldos em ordem crescente \n 6- Sair"));
            switch (Control) {
                case 1:
                    numero = Integer.parseInt(JOptionPane.showInputDialog("Digite o numero da conta para Inserir o Recebimento: "));
                    recebimento = Double.parseDouble(JOptionPane.showInputDialog("Digite o valor do Recebimento: "));
                    for (i=0;i<3;i++) {
                        for (j=0;j<3;j++) {
                            if (numero == B[i][j]) {
                                n1 = i;
                                n2 = j;
                                C[n1][n2] = C[n1][n2] + recebimento;
                            }
                        }
                    }
                    JOptionPane.showMessageDialog(null, A[n1][n2]+" seu saldo atual é de: "+C[n1][n2]);
                    break;
                case 2:
                    numero = Integer.parseInt(JOptionPane.showInputDialog("Digite o numero da conta para Retirar o Pagamento: "));
                    pagamento = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor do Pagamento: "));
                    for (i=0;i<3;i++) {
                        for (j=0;j<3;j++) {
                            if (numero == B[i][j]) {
                                n1 = i;
                                n2 = j;
                                C[n1][n2] = C[n1][n2] - pagamento;
                            }
                        }
                    }
                    JOptionPane.showMessageDialog(null, A[n1][n2]+" seu saldo atual é de: "+C[n1][n2]);
                    break;
                case 3:
                    //3–Mostre em apenas uma mensagem todos os Números de Conta, Correntistas e Saldos respectivos.
                    break;
                case 4:
                    for (i=0;i<3;i++){
                        for (j=0;j<3;j++){
                            if (maior_saldo < C[i][j]){
                                maior_saldo = C[i][j];
                                n1 = i;
                                n2 = j;
                            }
                        }
                    }
                    JOptionPane.showMessageDialog(null,"O Correntista mais rico é:\n Correntista: "+A[n1][n2]+"\n Número da conta:  "+B[n1][n2]+"\n Saldo: "+C[n1][n2]);
                    break;
                case 5:
                    for (i=0;i<3;i++){
                        for (j=0;j<3;j++){
                            Ordenar_C[k] = C[i][j];
                            k++;
                        }
                    }
                    troca = 0;
                    fim = 9-1;
                    while(troca==0){
                        troca=1;
                        for (k=0;k<fim;k++){
                            if (Ordenar_C[k] > Ordenar_C[k+1]){
                                aux = Ordenar_C[k];
                                Ordenar_C[k] = Ordenar_C[k+1];
                                Ordenar_C[k+1] = aux;
                                troca=0;
                            }
                        }
                        fim = fim-1;
                    }
                    for (k=0;k<9;k++){
                        JOptionPane.showMessageDialog(null,"Os saldos ordenados em forma crescente ("+k+"): \n"+Ordenar_C[k]);
                    }
                    break;
                case 6:
                    flag=1;
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "Algo de errado não esta certo ;3");
            }
        }
    }
}

My problem is case 3: "Show in just one message all Account Numbers, Account Holders and respective Balances". My teacher told me:

-"Use text concatenation on a variable. Then only a showmessage".

-"Tip: for to concatenate text after a showmessage".

I haven’t really understood yet how I could do this but if possible I’d like to put:

Name: +A

Account number: +B

Balance: +C

But having to use only one message to it confused me a little bit, and I would like to know what I could do to show this information without using the indexes (e.g.: + A[0][0], Numero Conta: + B[0][0], Balance: + C[0][0]).

  • Because it has to be done without using the indexes?

  • Because if I used larger matrices I could still use this code.

1 answer

1


I would say that the teacher has already given all the necessary tips:

"Use text concatenation in a variable. Then only one showmessage".

"Tip: for to concatenate text after a showmessage".

Then first you create a variable that will get the message. Then use two loops for to scroll through the "matrices" (are actually arrays of arrays, but anyway), and go concatenating the respective values in this variable. As they are several concatenations of String, I suggest using a StringBuilder:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < A.length; i++) {
    for (int j = 0; j < A[i].length; j++) {
        sb.append(A[i][j]).append(" ").append(B[i][j]).append(" ").append(C[i][j]).append("\n");
    }
}
String mensagem = sb.toString();

And there, at the end the variable mensagem will have all data concatenated. I put a space separating the name, account number and balance, and put a line break at the end (the \n). The message went like this:

Tibúrcio 111 0.0
Ricardo 222 0.0
João 333 0.0
Felisbino 444 0.0
Seu Zé 555 0.0
Dona Maria 666 0.0
Gedonstrôncio 777 0.0
Batman 888 0.0
Homem de Ferro 999 0.0

You can change the message however you want (like putting a comma separating the data, or any other format you want). And once you have the message, just show it with JOptionPane.showMessageDialog (or whatever you’re using to display messages).

It is worth saying that there is no escape from using the indexes, because you want to concatenate A[i][j] with B[i][j] with C[i][j]. Not knowing the values of i and j, cannot know which data will be concatenated in the correct order.


Of course you could also do without one StringBuilder, creating the String and concatenating directly into it:

String mensagem = "";
for (int i = 0; i < A.length; i++) {
    for (int j = 0; j < A[i].length; j++) {
        mensagem += A[i][j] + " " + B[i][j] + " " + C[i][j] + "\n";
    }
}

But when concatenating into one loop, it is more efficient to use a StringBuilder, as explained here.


That code assumes that A, B and C have the same amount of elements (so much so that I use A.length - the size of the array A - without checking whether B and C are the same size). If the quantity is different, you would have to check first if the sizes are equal and treat them accordingly (it does not seem to be the case for the exercise), so that the error of index out of bound.

Finally, I suggest using the Java code conventions and use variable names that start with lower case letters. And try to give more meaningful names as well (for example: instead of A, B and C, could be nomes, contas and saldos). It may sound like a silly detail, but better names help when programming.

Browser other questions tagged

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