0

I have an ArrayList that I want to print in decimal and binary format.

Current output: decimal: S:2 S:3 S:3 S:3 S:1 S:2

Expected:

decimal: 2 3 3 3 1 2 binary : 10 11 11 11 1 10

Any help is appreciated on how I can accomplish this.

I get this error "Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method toBinaryString(int) in the type Integer is not applicable for the arguments (List)at generics.ExampleGenerics.main(ExampleGenerics.java:50)"

I am learning generics, do I need to cast an int to the string array to get this to work? or am I way off?

public class ExampleGenerics {
    public static void main(String[] args) {
        List < Square > squareList = new ArrayList < > (Arrays.asList(new Square(1),
            new Square(2), new Square(2), new Square(3), new Square(3), new Square(3)));
        System.out.println("original squareList: " + squareList);

        Collections.rotate(squareList, -2);
        System.out.println("rotated list: " + squareList);
        System.out.println(Integer.toBinaryString(squareList)); //error 
    }
}

public class Square {
    private int side;

    public Square(int side) {
        this.side = side;
    }

    public int getSide() {
        return side;
    }

    public void setSide(int side) {
        this.side = side;
    }

    @Override
    public String toString() {
        return "S:" + side;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + side;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Square))
            return false;
        Square other = (Square) obj;
        if (side != other.side)
            return false;
        return true;
    }
}
Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
Bob
  • 23
  • 6

1 Answers1

0

The issue is that you're attempting to pass your List to the method Integer.toBinaryString(). However, this method doesn't take Lists, it takes ints. This is stated in the exception: "The method toBinaryString(int) in the type Integer is not applicable for the arguments (List)".

Instead of passing in your List, you need to loop through it and pass in the int for each Square object. So instead of doing this:

System.out.println(Integer.toBinaryString(squareList));

Do this:

for(Square square:squareList){
    System.out.println(Integer.toBinaryString(square.getSide()));
}
Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
  • thank you! I was thinking I needed a for each loop. One last question, Looking at the Java Integer API there is a toBinaryString, toHexString method etc. However, I don't see a way to convert to decimal. What would you suggest, can you point me in the right direction? – Bob Mar 21 '18 at 16:31
  • What do you mean by decimal? Are you referring to fractions? for instance 1.23? If that's what you're going for, you don't want to use `int`, you want `double`. Reading up on the [primitive data types in Java](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) might prove useful. You might also want to read [this Stackoverflow post](https://stackoverflow.com/a/10064845/8972283). Further, [this post](https://stackoverflow.com/a/13252940/8972283) details how to convert an `int` to a `double`. – Francis Bartkowiak Mar 21 '18 at 16:38
  • Thank you once again, I found what I was looking for with those links :) – Bob Mar 21 '18 at 16:44