Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to return more than one value from a method in Java? [duplicate]

I am using a simulator to play craps and I am trying to return two values from the same method (or rather I would like to).

When I wrote my return statement I simply tried putting "&" which compiled and runs properly; but I have no way of accessing the second returned value.

public static int crapsGame(){
  int myPoint;
  int gameStatus = rollagain;
  int d1,d2;
  int rolls=1;

  d1 = rollDice();
  d2 = rollDice();
  switch ( d1+d2 ) {
    case 7: 
    case 11:
      gameStatus = win;
      break;
    case 2: 
    case 3: 
    case 12:
      gameStatus = loss;
      break;
    default:
      myPoint = d1+d2;
      do {
        d1=rollDice();
        d2=rollDice();
        rolls++;
        if ( d1+d2 == myPoint )
          gameStatus = win;
        else if ( d1+d2 == 7 )
          gameStatus = loss;
       } while (gameStatus == rollagain);
  } // end of switch
  return gameStatus & rolls;
}

When I return the value as:

gameStatus=crapsGame();

It appropriately sets the varaible to win or lose but if I try something as simple as following that statement with:

rolls=crapsGame();

It is assigned the same value as gamestatus...a 0 or a 1 (win or lose). Any way that I can access the second returned value? Or is there a completely different way to go about it?

like image 844
Michael MacDonald Avatar asked Mar 08 '13 01:03

Michael MacDonald


People also ask

How can you return more than one value from a method?

If we want the function to return multiple values of same data types, we could return the pointer to array of that data types. We can also make the function return multiple values by using the arguments of the function.

Can return return 2 values?

You can't return two values. However, you can return a single value that is a struct that contains two values.

Can we have 2 return types in Java?

No, you don't have two return types. It's a generic method you are seeing.

Can a method return a double?

The type of the expression in the return statement must match the return type of the method. When you declare that the return type is double , you are making a promise that this method will eventually produce a double value.


2 Answers

Create your own value holder object to hold both values, then return it.

return new ValueHolder(gameStatus, rolls);

It's possible to return an array with multiple values, but that's cryptic and it does nothing for readability. It's much easier to understand what this means...

valueHolder.getGameStatus()

than what this means.

intArray[0]
like image 107
rgettman Avatar answered Sep 19 '22 14:09

rgettman


returning gameStatus & rolls means "return the bitwise and of gameStatus and rolls" which probably is not what you want

you have some options here:

  • return an array
  • create a class that represents the response with a property for each value and return an instance
  • use one of the many java collections to return the values (probably lists or maps)
like image 45
Francisco Meza Avatar answered Sep 19 '22 14:09

Francisco Meza