Jump to content

Erro: array index out of bounds


Francisco Barril

Recommended Posts

package laprahp;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
//import org.la4j.Matrix;
//import org.la4j.Vector;
//import org.la4j.matrix.DenseMatrix;
//import org.la4j.matrix.dense.Basic2DMatrix;
//import org.la4j.decomposition.EigenDecompositor;


public class laprahp {
    private final static double N_CRITERIOS = 3;
    private final static double N_ALTERNATIVAS = 4;
    private final static double IR=0.58;
        static Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        String[]total = new String [19]; 
        double[][]m1 = new double[3][3];
        double[][]m2 = new double[4][4];
        double[][]m3 = new double[4][4]; 
        double[][]m4 = new double[4][4];
        double [][]m1n=new double[3][3];
        double[][]m2n=new double [4][4];
        double[][]m3n=new double [4][4];
        double[][]m4n=new double [4][4];
        double [][]pre=new double[3][1];
        double [] Ax=new double[3];
        double LamMax=0;
        double IC=0;
        double RC=0;
        System.out.println("introduza o nome do ficheiro .txt");
        String nome =input.nextLine();
        lerFicheiroInput(nome,total);
        preencherMatrizCriterios(total,m1);
        preencherMatrizesMCP(total,m2,m3,m4);
//        for(int i=0;i<m1.length;i++){
//            for(int j=0;j<m1[0].length;j++){
//                System.out.print(m1[i][j]);
//             System.out.println("    ");
//            }
//        }
        m1n=m1n(m1); 
//        for(int i=0;i<m1n.length;i++){
//            for(int j=0;j<m1n[0].length;j++){
//                System.out.print(m1n[i][j]+" ");
//             System.out.println("   ");
//            }
//            }
        pre=PRe(m1n,pre);
//         for(int i=0;i<pre.length;i++){
//            for(int j=0;j<pre[0].length;j++){
//                System.out.print(pre[i][j]+" ");
//             System.out.println("    ");
//            }
//            }
            
         Ax=AxB(m1,pre);
//            for(int i=0;i<Ax.length;i++){
//            
//                System.out.print(Ax[i]+"  ");
//            } System.out.println("");
        LamMax=LamMax(Ax,pre,LamMax);
        RC=Rc(IC,LamMax,RC);
        m2n=m123n(m2);
                for(int i=0;i<m2n.length;i++){
            for(int j=0;j<m2n[0].length;j++){
                System.out.print(m2n[i][j]+" ");
             System.out.println("   ");
            }
            }
        m3n=m123n(m3);
        m4n=m123n(m4);
      
        

    }
    
    private static void lerFicheiroInput (String nomeFich, String[]total) throws FileNotFoundException {
        Scanner fInput = new Scanner(new File(nomeFich));
        int cont=0;
        while(fInput.hasNext()){
            String linha = fInput.nextLine();
            if (linha.length() > 0) {
                total[cont]=linha;
                cont++;
            }
        }
        fInput.close();
    }
    
    private static void preencherMatrizCriterios(String[]total, double[][]mc_criterios){
        for(int i=1;i<4;i++){
            mc_criterios[i-1]=tratarLinha(total[i],3);
        }
    }
    
    private static void preencherMatrizesMCP(String[]total,double[][]mcp_estilo,double[][]mcp_confiabilidade,double[][]mcp_consumo){
        for (int i=5;i<9;i++){
            mcp_estilo[i-5]=tratarLinha(total[i],4);
        }
        for (int i=10;i<14;i++){
            mcp_confiabilidade[i-10]=tratarLinha(total[i],4);
        }
        for (int i=15;i<19;i++){
            mcp_consumo[i-15]=tratarLinha(total[i],4);
        }
        
    }
    
    private static double[] tratarLinha(String linha, int n){
        double[]temp2=new double [n];
        String[] temp = linha.split(" ");
        int j=0;
        for(int i=0;i<temp.length;i++){
            if(!"".equals(temp[i])){
                if(temp[i].length()==1){
                    temp2[j]= Double.parseDouble(temp[i]);
                }else{
                    String[]temp3=temp[i].split("/");
                    double v1=Double.parseDouble(temp3[0]);
                    double v2=Double.parseDouble(temp3[1]);
                    temp2[j]=v1/v2;
                }
                j++;
            }
        }
    return temp2;
    }
    public static double[][] m1n (double[][]m1){
                                double soma1=0;
                                double soma2=0;
                        double soma3=0;
                        double [][]m1n=new double [3][3];
                        soma1=m1[0][0]+m1[1][0]+m1[2][0];
                        soma2=m1[0][1]+m1[1][1]+m1[2][1];
                        soma3=m1[0][2]+m1[1][2]+m1[2][2]; 
                        double [] somas=new double [3];
                        somas[0]=soma1;
                        somas[1]=soma2;
                        somas[2]=soma3;
                        for(int j=0;j<m1[0].length;j++){
                        for(int i=0;i<m1.length;i++){
                        m1n[i][j]=m1[i][j]/somas[j];
                                             
                                          }
                                      }
                                      return m1n;                                      
    }
    public static double [][] PRe (double[][] m1n,double [][]pre){
        
        pre[0][0]=(m1n[0][0]+m1n[0][1]+m1n[0][2])/3;
        pre[1][0]=(m1n[1][0]+m1n[1][1]+m1n[1][2])/3;
        pre[2][0]=(m1n[2][0]+m1n[2][1]+m1n[2][2])/3;
        return pre;
    }
    public static double[] AxB(double m1[][], double pre[][]) {
        double Ax[]= new double[m1.length];
       // if (a.length == b[0].length) {
            for (int a = 0; a < m1.length; a++) {
                for (int b = 0; b < m1[0].length; b++) {
                    Ax[a] = Ax[a] + (m1[a][b] * pre[b][0]);
                }
            }
   //     }
        return Ax;
    }
    
    public static double LamMax (double [] Ax,double [][]pre,double LamMax){
      double aux=0;
      double aux1=0;
      double aux2=0;
      aux=Ax[0]/pre[0][0];
      aux1=Ax[1]/pre[1][0];
      aux2=Ax[2]/pre[2][0];
      LamMax=(aux+aux1+aux2)/3;
      return LamMax;
    }
    public static double Rc (double IC,double LamMax,double RC) {
        IC=(LamMax-N_CRITERIOS)/(N_CRITERIOS-1);
        RC=IC/IR;
        return RC;
               
    }
     public static double[][] m123n (double[][]m1){
                        double soma1=0;
                        double soma2=0;
                        double soma3=0;
                        double soma4=0;
                        double [][]m1n=new double [3][3];
                        soma1=m1[0][0]+m1[1][0]+m1[2][0]+m1[3][0];
                        soma2=m1[0][1]+m1[1][1]+m1[2][1]+m1[3][1];
                        soma3=m1[0][2]+m1[1][2]+m1[2][2]+m1[3][2]; 
                        soma4=m1[0][3]+m1[1][3]+m1[2][3]+m1[3][3];
                        double [] somas=new double [4];
                        somas[0]=soma1;
                        somas[1]=soma2;
                        somas[2]=soma3;
                        somas[3]=soma4;
                        for(int j=0;j<m1[0].length;j++){
                        for(int i=0;i<m1.length;i++){
                        m1n[i][j]=m1[i][j]/somas[j];
                                             
                                          }
                                      }
                                      return m1n;                                      
    }

ignorem o que esta em comentário, ao correr o programa da erro na linha 207     m1n[ i ][j]=m1[ i ][j]/somas[j]; arrayindexoutofbounds exception : 3 ;

alguém me consegue explicar ou corrigir ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.