0

Im trying to print out my array without all of the brackets and commas so I'm attempting to override the toString() method; However im getting an output of [I@5c647e05. Sorry if my post has bad form this is my first post here. Im trying to display the array as a binary number. Ive tried the toString() but it contains brackets and commas and i cant have that.

Here is my code.

import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
import java.util.*;

public class halfAdder {
    public static int[] binary = new int[2];

    public static void main(String[] args) {
        // Declaring booleans for the adder
        int sum = 0;
        int carry = 0;
        boolean a = false;
        boolean b = false;
        int tempA = 0;
        int tempB = 0;
        int notA = 0;
        int notB = 0;


        // Collecting all the information needed from the user
        Scanner console = new Scanner(System.in);

        System.out.print("Please enter your input for A: ");
        tempA = console.nextInt();
        System.out.print("Please enter an input for B: ");
        tempB = console.nextInt();


        // Deciding if what we collected as an input is either 0 or 1
        if(tempA == 0)
        {
            a = false;
            notA = 1;
            System.out.println("A hit first");
        }
        else
        {
            System.out.println("hit second");
            a = true;
            notA = 0;
        }

        if(tempB == 0)
        {
            System.out.println("B hit first");
            b = false;
            notB = 1;
        }
        else
        {
            System.out.println("B hit second");
            b = true;
            notB = 0;
        }

        sum = (notA*tempB)+(tempA*notB);

         if(tempA == 1 && tempB == 1)
        {
            carry = 1;
            sum = 0;
        } 

        binary[0] = carry;
        binary[1] = sum;


        System.out.println("a = " + tempA);
        System.out.println("b = " + tempB);
        System.out.println("not a = " + notA);
        System.out.println("not b = " + notB);
        System.out.println("Sum = " + sum);
        System.out.println("Carry = " + carry);
        System.out.println(binary.toString());
    }

    public String toString()
    {
        String binaryNumber = "The binary number is: ";

        for(int i = 0; i < binary.length; i++)
        {
            binaryNumber += binary[i];
        }
        return binaryNumber;
    }
}
Jordanthedud
  • 61
  • 1
  • 6

2 Answers2

1

If you wish to convert an array into a String, use the Arrays class:

String arrayAsString = Arrays.toString(binary);

There are many ways to display the contents of the array like a binary number (without the comma's and brackets that the above method adds). If using Arrays.toString, you can parse the returned String using the methods provided in the String class. Alternatively, you can just use a StringBuilder (or String addition) and loop over the array.

copeg
  • 8,290
  • 19
  • 28
0

Check out the Arrays.toString() method for outputting an array as a string with one line of code. To remove the commas and brackets, use the String.replace() method.