Algorithm
#Algorithm
TheThe other answers are right to say that you should use Integer.toBinaryString(...)
, but if you have to implement the binary conversion yourself you'd be better off using bitwise operators. Those would allow you to support negative numbers as well as positive. You could also save time and lines by counting 1s and 0s as you add them to the result string.
#Practice
You're
Practice
You're using a @SuppressWarnings("resource")
annotation instead of closing your scanner. You should close it or, if you're using Java 7, use a try-with-resources
statement instead. This will make sure your scanner gets closed.
#Code
You
Code
You use 3 different int
variables which you create at different points in your code. It'd be better to put them in the same definition, or at least near each other. You can get better performance out of a StringBuilder
object rather than concatenating to the same string. It's not really an issue for such simple code, but it might be a problem in the future.
#Algorithm
The other answers are right to say that you should use Integer.toBinaryString(...)
, but if you have to implement the binary conversion yourself you'd be better off using bitwise operators. Those would allow you to support negative numbers as well as positive. You could also save time and lines by counting 1s and 0s as you add them to the result string.
#Practice
You're using a @SuppressWarnings("resource")
annotation instead of closing your scanner. You should close it or, if you're using Java 7, use a try-with-resources
statement instead. This will make sure your scanner gets closed.
#Code
You use 3 different int
variables which you create at different points in your code. It'd be better to put them in the same definition, or at least near each other. You can get better performance out of a StringBuilder
object rather than concatenating to the same string. It's not really an issue for such simple code, but it might be a problem in the future.
Algorithm
The other answers are right to say that you should use Integer.toBinaryString(...)
, but if you have to implement the binary conversion yourself you'd be better off using bitwise operators. Those would allow you to support negative numbers as well as positive. You could also save time and lines by counting 1s and 0s as you add them to the result string.
Practice
You're using a @SuppressWarnings("resource")
annotation instead of closing your scanner. You should close it or, if you're using Java 7, use a try-with-resources
statement instead. This will make sure your scanner gets closed.
Code
You use 3 different int
variables which you create at different points in your code. It'd be better to put them in the same definition, or at least near each other. You can get better performance out of a StringBuilder
object rather than concatenating to the same string. It's not really an issue for such simple code, but it might be a problem in the future.
#Algorithm
The other answers are right to say that you should use Integer.toBinaryString(...)
, but if you have to implement the binary conversion yourself you'd be better off using bitwise operators. Those would allow you to support negative numbers as well as positive. You could also save time and lines by counting 1s and 0s as you add them to the result string.
#Practice
You're using a @SuppressWarnings("resource")
annotation instead of closing your scanner. You should close it or, if you're using Java 7, use a try-with-resources
statement instead. This will make sure your scanner gets closed.
#Code
You use 3 different int
variables which you create at different points in your code. It'd be better to put them in the same definition, or at least near each other. You can get better performance out of a StringBuilder
object rather than concatenating to the same string. It's not really an issue for such simple code, but it might be a problem in the future.