Jump to content

Erro "NumberFormatException: empty String"


Francisco Barril

Recommended Posts

package ProjetoLapr;

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 ProjetoLapr{
    private final static double N_CRITERIOS=3;
    private final static double N_ALTERNATIVAS=4;
    private final static double IR3=0.58;
    static Scanner input = new Scanner(System.in);
    public static void main(String [] args) throws FileNotFoundException {
                

        String [] tudo = new String [19];
        double [][] mcCriterios= new double [3][3];
        double [][] mcp1 = new double [4][4];
        double [][] mcp2= new double [4][4];
        double [][] mcp3= new double [4][4];
        System.out.println("Insira o nome do ficheiro .txt");
        String nomeFich=input.nextLine();
        LerFicheiro(nomeFich,tudo);
        preencherMCRITERIOS(tudo,mcCriterios);
        preencherM123(tudo,mcp1,mcp2,mcp3);
        mostrarMatriz(mcCriterios,mcp1,mcp2,mcp3);
    




}
    public static void LerFicheiro (String nomeFich,String [] tudo) throws FileNotFoundException{
       // metodo para ler os dados do ficheiro de entrada e colocar num vetor(exclui cabecalhos)
        Scanner lerFich = new Scanner(new File(nomeFich));
        int cont=0;
        while(lerFich.hasNext()){
            String linha=lerFich.nextLine();
            //linha=linha.trim();
            if(linha.length()>0){
               //if (Character.isDigit(linha.charAt(0))) {
                    // se tiver conteudo e nao for leta o 1o carater ( evita cabecalho)
                    tudo[cont]=linha;
                    cont++;
                    
                //}
            }
           
    } lerFich.close();
    }
   public static void preencherMCRITERIOS (String [] tudo,double [][]mcCriterios){
       // apenas para preencher a de criterios pois é 3x3 e as outras sao 4x4
       for (int i=1;i<4;i++){
           mcCriterios[i-1]=tratarLinha(tudo[i],3);
       }
    
}
   public static double [] tratarLinha (String linha,int x){
       //recebe a capacidade do vetor =x e trata a linha para colocar na respetiva matriz
       double []aux2=new double[x];
       String [] aux=linha.split(" ");
       int j=0;
       for(int i=0;i<aux.length;i++){
           if(!aux[i].equals(" ")){
              if(aux[i].length()==1){
                  // se nao for numero fracionario
                  aux2[j]=Double.parseDouble(aux[i]);
                  
              }else{
                  // se for numero fracionario transforma em decimal
                  String []aux3=aux[i].split("/");
                  double num=Double.parseDouble(aux3[0]);
                  double den=Double.parseDouble(aux3[1]);
                  aux2[j]=num/den;
              }    
                    j++;
           }
       }            return aux2;
   }    
   public static void preencherM123(String[]tudo,double[][]mcp1,double[][]mcp2,double[][]mcp3){
       for(int i=5;i<9;i++){
           mcp1[i-5]=tratarLinha(tudo[i],4);
       }
       for(int i=10;i<14;i++){
           mcp2[i-10]=tratarLinha(tudo[i],4);
       }
       for (int i=15;i<19;i++){
           mcp3[i-15]=tratarLinha(tudo[1],4);
       }
   }
   public static void mostrarMatriz(double[][]mcCriterios,double[][]mcp1,double[][]mcp2,double[][]mcp3){
       System.out.println("que matriz deseja ver?(1-4)");
       int nMat=input.nextInt();
        switch (nMat) {
            case 1:
                for(int i=0;i<mcCriterios.length;i++){
                    for(int j=0;j<mcCriterios[0].length;j++){
                        System.out.println(mcCriterios[i][j]);
                    }
                }
            case 2:
                 for(int i=0;i<mcp1.length;i++){
                    for(int j=0;j<mcp1[0].length;j++){
                        System.out.println(mcp1[i][j]);
                    }
                 }
            case 3:
                for(int i=0;i<mcp2.length;i++){
                    for(int j=0;j<mcp2[0].length;j++){
                        System.out.println(mcp2[i][j]);
                    }
                 }
            case 4:
                 for(int i=0;i<mcp3.length;i++){
                    for(int j=0;j<mcp3[0].length;j++){
                        System.out.println(mcp3[i][j]);
                    }
                 }
        }
        
    }
   }

ao correr o programa dá-me erro na linha 75 (double num=Double.parseDouble(aux3[0]);) ;

Citação

Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at ProjetoLapr.ProjetoLapr.tratarLinha(ProjetoLapr.java:75)
    at ProjetoLapr.ProjetoLapr.preencherMCRITERIOS(ProjetoLapr.java:57)
    at ProjetoLapr.ProjetoLapr.main(ProjetoLapr.java:27)
C:\Users\Utilizador\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
 

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.