Search code examples
javaarraysio

Why i can get only last element of array outside while loop


Why i get only last element of array outside while loop ? How i can get all elements in array outside the loop ?

for(i=0;i<N;i++)
       {
           while(s.hasNext() && s2.hasNext())
           {
               String xx= s.next();
               String yy= s2.next(); double val1 = Double.parseDouble(xx);
               double val2 = Double.parseDouble(yy);
               x[i]=val1;
               y[i]=val2; 
               //not work
               result=DoConv(x, y, M, L, N);
               System.out.println(result[i]);
           }


       }

   }
   else
   {
       System.out.println("Different number of elements in file.");
   }
   for(i=0;i<3;i++)
   {
       // prints only last element
       System.out.println(x[i]);

   }

Solution

  • because the index i is not updating. I assume you are getting the last element for the whole array. you need to increment i inside while loop too