Return of two values in JAVA


I am working on a decimal to binary converter and vice versa all this must go within the same method the problem is that I do not know how to return the binary value and the decimal one I leave the code waiting for some help

package numerosBinariosYDecimales;

import java.util.Scanner;

public class BinariosYDecimales {
    public static void main (String[]args){
        Scanner lecturaDatos = new Scanner(;
        System.out.println("Ingresa un numero decimal");
        long decimal =lecturaDatos.nextLong();
        long auxiliar = decimal;
        String binario="";

        while (auxiliar >0){
            binario = auxiliar %2 + binario;
            auxiliar /=2;

        System.out.println("El numero decimal "+decimal+" en binario es: "+binario);

        //conversion de binario a decimal

        long numBinario = Long.parseLong(binario);
        long numDecimal=0;
        int contador =1;
        long auxiliarDecimal;
        while (numBinario >0){
            auxiliarDecimal = numBinario %2;
            numDecimal = numDecimal + auxiliarDecimal * contador;
            numBinario /=10;
            contador = contador*2;

        System.out.println("El numero binario "+binario+" es "+decimal+" en numeros decimales");

asked by cocoas 24.07.2018 в 01:36

2 answers


You can return an array of two elements, in one position you save the decimal value and in the other the binary.

answered by 24.07.2018 в 01:51

I have found this case enough times to make a small class that allows to group two values. Here it is:

import java.util.Objects;

public final class Pair<L, R> {

    private L left;
    private R right;

    public Pair(L left, R right) {
        this.left = left;
        this.right = right;

    // permite crear una Pair sin especificar los tipos, asi: Pair.of("hola", 654)
    public static <L, R> Pair<L, R> of(L left, R right) {
        return new Pair<>(left, right);

    public L getLeft() {
        return left;

    public Pair<L, R> setLeft(L left) {
        return new Pair<>(left, right);

    public <T> Pair<T, R> updateLeft(T left) {
        return new Pair<>(left, right);

    public R getRight() {
        return right;

    public Pair<L, R> setRight(R right) {
        return new Pair<>(left, right);

    public <T> Pair<L, T> updateRight(T right) {
        return new Pair<>(left, right);

    public String toString() {
        return "(" + left + ", " + right + ")";

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Pair<?, ?> pair = (Pair<?, ?>) o;
        return Objects.equals(left, pair.left) &&
                Objects.equals(right, pair.right);

    public int hashCode() {
        return Objects.hash(left, right);

And its use in your case:

int binario = 0;
int decimal = 0;

// aqui tu logica

return Pair.of(binario, decimal);

// y en otro punto del programa

int a = numeros.getLeft();
int b = numeros.getRight();

System.out.println(numeros);    // imprime (0, 0)


It is true that these types of structures are implemented in one way or another in many libraries, such as Apache or Vavr , but sometimes you just need this functionality and using a library is too much.

answered by 24.07.2018 в 09:21