-1

how can I return two values in java ?

for (int i = 1; i < prob.length; i++) {

             x=x+xprob[i];
             y=y+yprob[i];

      }

       return x,y;

thanks

Izzy
  • 1
  • 1

1 Answers1

1

Either return an isntance of a class whose attributes are x and y, or an array of two elements.

Example :

public int[] func ()
{
    int[] arr = new int[2];
    ...
    arr[0] = x;
    arr[1] = y;
    return arr;
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • do you have an example how to return an array plz – Izzy Dec 27 '14 at 19:45
  • I got this an error Exception in thread "main" java.lang.NullPointerException – Izzy Dec 27 '14 at 20:05
  • @Izzy You'll have to show me the code where you got this exception. – Eran Dec 27 '14 at 20:06
  • public static void main(String[] args) { // TODO Auto-generated method stub setUpMyCSVArray (); double arr1[][] = new double[36][29]; double arr2[][] = new double[3][27]; seriesDistance(arr1, arr2); } – Izzy Dec 27 '14 at 20:12
  • btw, I return the two value in this way String xString = Double.toString(x); String yString = Double.toString(y); String xyvalue="the value of x is"+ xString +"the value of y is" + yString ; return xyvalue; } – Izzy Dec 27 '14 at 20:14
  • @Izzy what about the code of `seriesDistance` and `setUpMyCSVArray ();`? the exception is probably thrown by one of them. – Eran Dec 27 '14 at 20:16
  • Yup it's in the seriesDistance ,what would u suggest to make it like an object – Izzy Dec 27 '14 at 20:21