0

I want to add randomly generated numbers to an vector with size that is determined by the user(xdata, ydata) by using my addInt method, it generates the required numbers in my debugging, but it only returns 0, 0, .. when I print it out

  System.out.println("Input the size of first array");
  int[] xdata = new int[sc.nextInt()];
  System.out.println("Input the size of second array");
  int[] ydata = new int[sc.nextInt()];

  /* this code got my desired results but I want to use a single method
  for(int i=0; i<xdata.length; i++)
     xdata[i]= (int)(Math.random() * 100);
  for(int i=0; i<ydata.length; i++)
     ydata[i]= (int)(Math.random() * 100);
     */

  IntVector x = new IntVector(xdata); //intVector is my class and contructor
  IntVector y = new IntVector(ydata); //I had to make for this

  x.addInt(xdata);
  y.addInt(ydata);

  System.out.println(x.toString()+"\nsorted: ");
  System.out.println(y.toString());



  ////*****my adding method********////
public class IntVector{
//vector constructor//
  public int[] addInt (int[] data){
  if (data.length == n) {  //n meaning my capacity
     IntVector tmp[] = new IntVector[n+10000]; 

     for(int i=0; i<data.length; i++)
        data[i]= (int)(Math.random() * 100);

    } return  data; 
      //all data[i] have randomly generated numbers
      //but it only prints out 0 for each value after.
   }                
 }
Marky_rick
  • 13
  • 3
  • What is `IntVector`? What do you mean by _"I want to use a method"_? Do you mean you want to add a method to `IntVector`? Sorry, but the question is very unclear. – Jim Garrison Dec 02 '17 at 01:09
  • I have a method to add to the vector, but it's not returning the array just all zeros. – Marky_rick Dec 02 '17 at 01:14
  • @Marky_rick - I don't think we will be able to understand what you are doing / trying to do without an MCVE. For a start, your `addInt` method doesn't make much sense. What is `n` about? What is `tmp` about? Why are you passing and modifying `data`? – Stephen C Dec 02 '17 at 01:21

0 Answers0