Vetor bidimensional. Duvida

Tenho o seguinte problema que nao to conseguindo resolver…

O codigo tem que ler duas matrizes de 7 elementos. Tenho que construir uma matriz C com duas dimensoes: a primeira coluna tera todos os dados digitados na matriz A e a segunda coluna na matriz B.

O que esta errado no codigo??

import javax.swing.JOptionPane;   
class Exercicio8 {  
     public static void main(String arg[]) {  
         int A[][] = new int[7][1];
         int B[][] = new int[7][1];  
         int C[][] = new int [7][2];
         
         for (int i = 0; i < 7; i++) {
        	  for (int j = 0; j < 1; j++){
        		  A[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor A ["   + i + "] [ "+j+" ] "));  
              }  
          
         }   
        
         for (int i = 0; i < 7; i++) {  
             for (int j = 0; j < 1; j++) {  
                B[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor B ["+ i + "] [ "+j+" ] "));  
             }  
          }  
         for (int i = 0; i < 7; i++) {  
             for (int j = 0; j < 2; j++) {  
                 C [i][j]= A[i][j] ; 
                 C[i][j] = B[i][j]  ;                 
      }        
  }  
         for (int i = 0; i < 7; i++) {  
             for (int j = 0; j < 1; j++) {  
                 System.out.print(C [i][j]+" ");                
      }     
             System.out.print("\n");  
  }  
        
  }  
}

for (int i = 0; i < 7; i++) { for (int j = 0; j < 2; j++) { C [i][j]= A[i][j] ; C[i][j] = B[i][j] ; }

Aqui você está setando 2 valores na posição C[i][j], enào só o segundo irá aparecer… Você precisa percorrer todas as matrizes A e B, e setar o valor na C, não o contrário

eu tenho que fazer assim: A[i][j] = C[i]?

Não… tenta entender melhor o post do thegoergen. =)

Voce está passando valores dos dois vetores A e B para o mesmo elemento de C.

Tentei fazer assim mais teria que imprimir um do lado do outro…

import javax.swing.JOptionPane;   
class Exercicio8 {  
     public static void main(String arg[]) {  
         int A[][] = new int[7][1];
         int B[][] = new int[7][1];  
         int C[][] = new int [7][2];
         
         for (int i = 0; i < 7; i++) {
        	  for (int j = 0; j < 1; j++){
        		  A[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor A ["   + i + "] [ "+j+" ] "));  
              }  
          
         }   
        
         for (int i = 0; i < 7; i++) {  
             for (int j = 0; j < 1; j++) {  
                B[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor B ["+ i + "] [ "+j+" ] "));  
             }  
          } 
         String AC ="";
         for (int i = 0; i < 7; i++) {  
             for (int j = 0; j < 1; j++) {  
                 C [i][j]= A[i][j] ; 
            AC += "C[" +i+ "][" +j+ "]=" + C[i][j] + "\n";                  
      }        
  }  
         String BC="";
         for (int i = 0; i < 7; i++) {  
             for (int j = 0; j < 1; j++) {  
                 C [i][j]= B[i][j] ; 
            BC += "C[" +i+ "][" +j+ "]=" + C[i][j] + "\n";                  
      }        
  }  
   
     	
 		JOptionPane.showMessageDialog(null,AC);
 		JOptionPane.showMessageDialog(null,BC);
     }
     }

Olhei melhor o código e reparei em algo: porque você usa matrizes para o A e B?? Se eles só tem uma “linha” mesmo… Pode Usar Vetor unidimensional:

String[] A = new String[7];
String[] B = new String[7];
String[][] C = new String[7][2];

ACho que ifcaria bem mais simples

Voce pode efetuar a atribuição e depois a impressão dos mesmo lado a lado.

Voce não precisa e 2 for, para o array A voce utiliza a coluna 0 de C, e para o array B voce utiliza a coluna 1 de C.

Vc tem que usar apenas um jogo de looping duplo…
for a{
for b{
multi(a,b) = Entre com o valor de a,b…
}
}

Este cara já preenche completamente a matriz bidimensional.

Sendo a (1…2) e b (5…6) na tela vc terá:

Entre com o valor de 1,5
Entre com o valor de 1,6
Entre com o valor de 2,5
Entre com o valor de 2,6

E pronto!

Isso que eu consigo entender:

Como faço pra colocar na coluna 0 de C os valores de A…

C[0] = A[i]?

[quote=cristianequeiroz1]Isso que eu consigo entender:

Como faço pra colocar na coluna 0 de C os valores de A…

C[0] = A[i]?[/quote]
Isso mesmo!

Da pra melhorar bastante esse código ainda…

import javax.swing.JOptionPane;   
class Exercicio8 {  
     public static void main(String arg[]) {  
         int A[] = new int[7];
         int B[] = new int[7];  
         int C[][] = new int [7][2];
         
         for (int i = 0; i < 7; i++) {
		A[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor A [" + i + "]"));  
          
         }   
        
         for (int i = 0; i < 7; i++) {         
                B[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor B ["+ i + "]"));  
         } 
         
	 for (int i = 0; i < 7; i++) {
		C[i][0] = A[i];
		C[i][1] = B[i];
	 } 

	 for (int i = 0; i < 7; i++) {
		for(int j = 0; j < 2; j++) {
	 		System.out.println("C["+i+"]["+j+"] = "+ C[i][j]);
		}
         }
     	
     }
}

[quote=thegoergen][quote=cristianequeiroz1]Isso que eu consigo entender:

Como faço pra colocar na coluna 0 de C os valores de A…

C[0] = A[i]?[/quote]

for (int i = 0; i < A.length; i++){ C[0][i] = A[i]; } [/quote]

Acho que tem que inverter…

for (int i = 0; i < A.length; i++){ C[i][0] = A[i]; } [/quote]

[code]import javax.swing.JOptionPane;
class Exercicio8 {
public static void main(String arg[]) {
int A[] = new int[7];
int B[] = new int[7];
int C[][] = new int [7][2];

     for (int i = 0; i < 7; i++) {
	A[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor A [" + i + "]"));  
      
     }   
    
     for (int i = 0; i < 7; i++) {         
            B[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Digite o valor B ["+ i + "]"));  
     } 
     
	C[i][0] = A;
	C[i][1] = B;

 for (int i = 0; i < 7; i++) {
	for(int j = 0; j < 2; j++) {
 		System.out.println("C["+i+"]["+j+"] = "+ C[i][j]);
	}
     }
 	
 }

}
[/code]

[quote=GustavoLaguna][quote=thegoergen][quote=cristianequeiroz1]Isso que eu consigo entender:

Como faço pra colocar na coluna 0 de C os valores de A…

C[0] = A[i]?[/quote]

for (int i = 0; i < A.length; i++){ C[0][i] = A[i]; } [/quote]

Acho que tem que inverter…

for (int i = 0; i < A.length; i++){ C[i][0] = A[i]; } [/quote]
[/quote]

Não precisa do FOR. Funciona setando direto o Array.

C[i][0] = A;   
C[i][1] = B; 

Certeza? Acho que isso não compila.

Voce está tentando colocar um array onde é esperado um int.

Nao consigo mandar imprimir… ai esta errado a atribuicao?

Olhem como esta:

import javax.swing.JOptionPane;   
class Exercicio8 {  
     public static void main(String arg[]) {  
         int A[] = new int[7];
         int B[] = new int[7];  
         int C[][] = new int [7][2];
         
         for (int i = 0; i < 7; i++) {
 			String a = JOptionPane.showInputDialog(null, "Digite o valor A ["
 					+ i + "]");
 			A[i] = Integer.parseInt(a);
 		}
 		for (int i = 0; i < 7; i++) {
 			String b = JOptionPane.showInputDialog(null, "Digite o valor B ["
 					+ i + "]");
 			B[i] = Integer.parseInt(b);
 		}
 		  
 		    
 		 for (int i = 0; i < A.length; i++){  
 		 C[i][0] = A[i];  
 		 }  
 		
 		for (int i = 0; i < B.length; i++ ){
 			C[0] [i] = B[i];
 		}
 		
 		 String saida="";
 	      for (int i = 0; i < 7; i++) {  
 	         for(int j = 0; j < 2; j++) {  
 	        	saida += "C[" +i+ "][" +j+ "]=" + C[i][j] + "\n";	  
 	         }  
 	         
 	          }  
 	     JOptionPane.showMessageDialog(null,saida);
  
 	      }  
 	 } 

[quote=GustavoLaguna] C[i][0] = A; C[i][1] = B;

Certeza? Acho que isso não compila.

Voce está tentando colocar um array onde é esperado um int.[/quote]

Não assim. Assim:

C[0] = A;   
C[1] = B; 

Coloco um array de int onde é esperado um array de int. Testei antes, e funcionou. :slight_smile:

[quote=GustavoLaguna] C[i][0] = A; C[i][1] = B;

Certeza? Acho que isso não compila.

Voce está tentando colocar um array onde é esperado um int.[/quote]

opa editei por que vi mal estava olhando como se foce assim como thegoergen pos

    C[0] = A;     
    C[1] = B;   

[quote=thegoergen][quote=GustavoLaguna] C[i][0] = A; C[i][1] = B;

Certeza? Acho que isso não compila.

Voce está tentando colocar um array onde é esperado um int.[/quote]

Não assim. Assim:

C[0] = A;   
C[1] = B; 

Coloco um array de int onde é esperado um array de int. Testei antes, e funcionou. :slight_smile:
[/quote]

acho que não é o ideal também, voce vai criar um array de 7 elementos onde era um array de 2.

é a mesma coisa que fazer

C[0] = new int[7];
C[1] = new int[7];

[quote=cristianequeiroz1]Nao consigo mandar imprimir… ai esta errado a atribuicao?

Olhem como esta:

[code]
import javax.swing.JOptionPane;
class Exercicio8 {
public static void main(String arg[]) {
int A[] = new int[7];
int B[] = new int[7];
int C[][] = new int [7][2];

     for (int i = 0; i < 7; i++) {
		String a = JOptionPane.showInputDialog(null, "Digite o valor A ["
				+ i + "]");
		A[i] = Integer.parseInt(a);
	}
	for (int i = 0; i < 7; i++) {
		String b = JOptionPane.showInputDialog(null, "Digite o valor B ["
				+ i + "]");
		B[i] = Integer.parseInt(b);
	}
	  
	    
	 for (int i = 0; i < A.length; i++){  
	 C[i][0] = A[i];  
	 }  
	
	for (int i = 0; i < B.length; i++ ){
		C[0] [i] = B[i];
	}
	
	 String saida="";
      for (int i = 0; i < 7; i++) {  
         for(int j = 0; j < 2; j++) {  
        	saida += "C[" +i+ "][" +j+ "]=" + C[i][j] + "\n";	  
         }  
         
          }  
     JOptionPane.showMessageDialog(null,saida);

      }  
 } 

[/code][/quote]

Cris, aqui está errado:

for (int i = 0; i < B.length; i++ ){   
            C[0] [i] = B[i];   
        }   

invert… coloca

for (int i = 0; i < B.length; i++ ){   
            C[i] [1] = B[i];   
        }