So I'm trying to create a program that will take values from one array and multiply them with the values in the second array and then allocate the answer into a third array, then returning that array as a result

import java.util.Arrays;

public class ArrayCreator
{   

  public static void main( String[] args )
  {
    
   
    int [] L1;     
    int [] L2;     
    int n;         
    int m;         
    
    int index1;   
    int index2;   
    int index3;  
    int t;
    
    int [] L3;     
   

    System.out.println("Please input the first array.");
    L1=ITI1120.readIntLine();
    
    System.out.println("Please input the second array.");
    L2=ITI1120.readIntLine();
    
    //BODY
    n=L1.length;
    m=L2.length;
    L3=new int[n*m];
    t=(n*m);
    L3=L3Creator(L1,L2,L3);
   
  

    
   System.out.println(Arrays.toString(L3));
 
    
  }
  
  private static int[] L3Creator(int [] L1, int [] L2, int [] L3)
  {
   
    int index1;
    int index2;
    int index3;
    
    // BODY 
    
    for (index1=index2=index3=0;L3[index3]==L1[index1]*L2[index2];index3++)
  {
     if (index1<L1.length-1)
     {
       index1=index1+1;
     }
     else if (index1>=L1.length-1)
     {
       index1=0;
       index2=index2+1;
     }
    }
    
return L3;  
      }
}

but for some reason the program prints [0,0,0,0] regardless of what numbers I put.
Any help would be appreciated!

Recommended Answers

All 5 Replies

It never makes it into the loop in the 'L3Creator' method. Presumably 'L1[0] * L2[0]' will be non-zero, but 'L3[0]' will be zero, so the '==' test will return false.

I suspect that this is what you want:

for (index1 = index2 = index3 = 0; index3 < L3.length; index3++) {
			L3[index3] = L1[index1] * L2[index2];
			...

Small confusion about index1 and index2 here. AFAIKS you don't need 3 indexes, ie
L3 = L1*l2
... but if you do then you should be incrementing them all, not just index3.

ps The third array is just an output from this method - there's no point passing it in as an input parameter - just create it inside the method and return it.

thanks for all the help, I managed to get it working! how do I mark this thread as solved?:P

There's a hypertext link at the bottom of the thread that says something like "mark this thread solved". Just click it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.