Error adding the array with double data

3

The code below works as it should, in case anyone wants to use it, I thank all those who commented

Giving linear algebra are teaching me operations with vectors and to simplify my life I wanted to make a code for this, I am not very proficient with the code yet. Surfing the Internet I came across this for the java language.

import java.util.Scanner;
public class OperacionVectores{
public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    //Variables k seran el tamaño de los vectores
    int v1,v2,v3;
    System.out.print("Ingrese el tamaño del vector 1: ");
    v1=sc.nextInt();
    //creamos el primer vector con el tamaño de v1
    double vec1[]=new double[v1];
    //recorremos el vector v1 para llenarlo
    for(int i1=0;i1<vec1.length;i1++){
        System.out.print("\n ingrese el numero de la posicion "+i1+":");
        vec1[i1]=sc.nextDouble();
    }
    System.out.print("\n Ingrese el tamaño del vector 2: ");
    v2=sc.nextInt();
    //Creamos el primer vector con el tamaño de v1
    double vec2[]=new double[v2];
    //recorremos el vector v2 para llenarlo
    for(int i2=0; i2<vec2.length;i2++){
        System.out.print("ingrese el numero de la posicion "+i2+":");
        vec2[i2]=sc.nextDouble();
    }
    /*Determinamos cual de los dos vectores es mas grande,
    para k el vector 3 tenga el tamaño del mas grande*/
    v3=v1;
    if (v2 > v1)
        v3=v2;        
    //Declaramos el vector 3
    double vec3[]=new double[v3];
    //recorremos el vector 3 para hacer la suma
    for(int i3=0;i3<vec3.length;i3++){
        double valv1=0.0;
        if (vec1.length > i3){
            valv1 = vec1[i3];
        }
        double valv2=0.0;
        if(vec2.length > i3){
            valv2=vec2[i3];                
        }
        /*Hacemos la suma de las posiciones de los vectores 1 y 2 en las 
        posiciones del vector 3*/
        vec3[i3]=valv1*valv2;
        System.out.println("\nLa Suma de la Posicion"+" "+i3+" "+ "es:"+vec3[i3]);
    }
}

}

First the code is not mine, the only thing that I have changed, from my point of view, is the type of variable from int to double, because the vectors are decimals.

If I leave all the variables as integer the code works great, but when I do the change from int to double it tells me this

  

Exception in thread "main" java.lang.NullPointerException           at OperacionVectores.main (OperationVectores.java:40)

I think the problem is after the line that says v3 = v1 thank you very much for your help

    
asked by Barly Espinal 31.05.2018 в 11:03
source

1 answer

1

Your problem is that you make the comparison in the if just the opposite of how you are interested, here: if (vec1.length < i3){ valv1 = vec1[i3];} in vec1.length < i3 you are telling your comparator to do what is inside only if the index is greater than the length of the vector, when what you are interested in doing is just the opposite:

if (vec1.length > i3){ 
    valv1 = vec1[i3];
}

So whenever the index is less than the length of the array, you will operate with what is inside, otherwise you search in positions that do not exist.

I HOPE YOU HAVE BEEN SERVED:)

EDIT = > PS: in the following comparisons:

        Double valv1=0.0;
        if (vec1.length < i3){
            valv1 = vec1[i3];
        }
        Double valv2=0.0;
        if(vec2.length < vec2[i3]){
            valv2=vec2[i3];                
        }

I guess the purpose of what you want to achieve is the same, but the conditions do not compare the same things, the first condition compares the length of an array with the index to use, in the second you compare the length of another array, but with the value of the position i3 of that same array, I only point it out so that you have it in mind, that surely in the second comparison you will want to do the same as in the first, in which case, you must make the same change as in the first comparison:)

    
answered by 31.05.2018 / 11:21
source