1

I am trying to convert an integer to a binary number string, and I wrote the code for it and it compiled. However, I can't seem to write a test file for it, i keep getting an error. I am supposed to write a separate test file that outputs my answer, however I am not sure how to do that. I'm pretty new to Java. Can anyone help me figure out how to fix the error I get?

This is my java code for to convert it.

public String binaryNumber( int j)       
{

    String n = "";
    String a = "";


    do
    {
        a += (j % 2);
        j = j/2;         

    }while (j != 0);

    for(int r = (a.length() - 1); r >=0; r--)
     {
        n += a.charAt(r);
     }

    return n;
}

 public String getN {return n;}

This is my test code :

public class BinaryNumberTest
{
  public static void main(String[] args)
   {
    System.out.println("Result: " + binaryNumber(45));
   }

 }
1
  • Your binaryNumber must be static Commented Oct 9, 2015 at 20:42

3 Answers 3

1

Your code seems correct if you make the binaryNumber(int j) method static. Because you can't access non-static method inside static context.

However you can use Integer.toBinaryString(x) to perform the task easily. Additionally, you can use Integer.toString(x,8) to convert octal, Integer.toString(x,2) to convert binary, Integer.toString(x,16) to convert hexa-decimal, and Integer.toString(x,n) to n base.

Sign up to request clarification or add additional context in comments.

2 Comments

I assume this is a homework question, where the purpose of the task is to do it manually. So using Integer.toBinaryString(x) would defeat the purpose. :)
Thank you, changing it to static did work. I edited my test code and now I get an error again. I need to put an integer and then call out to it in my test code. Is there a way to do that?
0

You should make your binaryNumber method static because you are not using instance fields.

public class BinaryNumber 
{
    public static String binaryNumber( int j)       
    {

        String n = "";
        String a = "";


        do
        {
            a += (j % 2);
            j = j/2;         

        }while (j != 0);

        for(int r = (a.length() - 1); r >=0; r--)
         {
            n += a.charAt(r);
         }

        return n;
    }
}

To test:

public class BinaryNumberTest
{
  public static void main(String[] args)
   {
    System.out.println("Result: " + BinaryNumber.binaryNumber(45));
   }

 }

1 Comment

Thank you for helping. I edited my original post with changes to the test code. Could you help me with that?
0

First, since binaryNumber doesn't use any instance members it could (read: should) be defined as static. Second, you need to reference it by its class name, or statically import it:

System.out.println("Result: " + BinaryNumber.binaryNumber(45));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.