int to binary java
Java
String binary = Integer.toBinaryString(num);int n = 1000;
String s = Integer.toBinaryString(n);import java.util.*;
class Bin {
//fields
private Double number;
//constructors
public Bin(){
number = 0.0;
}
public Bin(Double num){
number = num;
}
//methods
public ArrayList<String> reverseArrayList(ArrayList<String> alist) {
ArrayList<String> array = new ArrayList<String>();
int x = 0;
int i = alist.size() - 1;
while(x < alist.size()){
array.add(alist.get(i));
i -= 1;
x += 1;
}
return array;
}
//getter methods
public ArrayList<String> getBinVal(){
ArrayList<String> binaryVal = new ArrayList<String>();
ArrayList<String> array = new ArrayList<String>();
Double num = number;
Double sol1, check;
while( num > 0 ) {
sol1 = num/2;
check = sol1 - Math.floor(sol1);
if(check > 0) {
binaryVal.add("1");
num = sol1 - 0.5;
}
else {
binaryVal.add("0");
num = sol1;
}
}
while(binaryVal.size() < 4) {
binaryVal.add("0");
}
array = reverseArrayList(binaryVal);
return array;
}
}
class MainClass {
public static void main(String[] args) {
//add the number in as a command line argument
Double num = Double.valueOf(args[0]);
//create an instance of Bin
Bin value = new Bin(num);
//print the binary value
System.out.println(value.getBinVal());
}
}
Also in Java:
- Title
- java doreturn void
- Category
- Java
- Title
- saving String character in arraylist
- Category
- Java
- Title
- java 8 add whole array to jsonarray
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- java create circle
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- protect java
- Category
- Java
- Title
- eliminar fila jtable java
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- how to check type of primitive value in java
- Category
- Java
- Title
- convert string to byte java
- Category
- Java
- Title
- how to add jar in maven java application in netbeans
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- NumSelfDivisors java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- java code to get all leaf nodes of a xml file
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- how to compare current date and time with another date and time in android
- Category
- Java
- Title
- java observable to observer stack overflow
- Category
- Java
- Title
- java math.random
- Category
- Java
- Title
- maximum arrays size in java
- Category
- Java
- Title
- how to remove components from a JFRame java
- Category
- Java
- Title
- the type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from required .class files
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- how to access private vairable in java method
- Category
- Java
- Title
- boolean checkbox swing
- Category
- Java
- Title
- lightweight java game library
- Category
- Java
- Title
- java console write
- Category
- Java
- Title
- java file class
- Category
- Java
- Title
- how to create a 2d arraylist java
- Category
- Java
- Title
- java map get the key from value
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- double to string java
- Category
- Java
- Title
- java hashmap set value
- Category
- Java
- Title
- count word in string no matter the delimiter java
- Category
- Java
- Title
- how to define an arraylist in java
- Category
- Java
- Title
- java map example
- Category
- Java
- Title
- setting up javafx in eclipse
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- int to string java
- Category
- Java
- Title
- are strings modifiable
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- print out value java
- Category
- Java
- Title
- java letter alphabet index
- Category
- Java
- Title
- compile java
- Category
- Java
- Title
- java new string array
- Category
- Java
- Title
- generate objects with for loop java
- Category
- Java
- Title
- StringBuilder java pop back
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- length of string java
- Category
- Java
- Title
- round no in java
- Category
- Java
- Title
- tower of hanoi program in java using recursion
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- java interface
- Category
- Java
- Title
- IN APPLet how to disable a button in java
- Category
- Java
- Title
- how to create java jframe in eclipse
- Category
- Java
- Title
- mkyong restful web services spring boot examples post
- Category
- Java
- Title
- replaceall in java
- Category
- Java
- Title
- prime factorization java
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- how to copy list item to another list in java
- Category
- Java
- Title
- how to play an audio in java
- Category
- Java
- Title
- how do you make a method that returns an array java
- Category
- Java
- Title
- java localdate subtract two dates
- Category
- Java
- Title
- generate random number java
- Category
- Java
- Title
- Java.awt graphics tutorial
- Category
- Java
- Title
- create instance object java
- Category
- Java
- Title
- capcitor FERR_CLEARTEXT_NOT_PERMITTED
- Category
- Java
- Title
- set password visible in android
- Category
- Java
- Title
- java foreach map
- Category
- Java
- Title
- java array to list
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- java square a number
- Category
- Java
- Title
- java 8 collect multiple lists into single list
- Category
- Java
- Title
- java infinitew recursion
- Category
- Java
- Title
- java get icon for file type
- Category
- Java
- Title
- how to make a pre set list java
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- java 8 function supplier consumer
- Category
- Java
- Title
- springBoot
- Category
- Java
- Title
- scan a string using scanner class in java
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- jtable fill panel
- Category
- Java
- Title
- checking if a given integer is a prime number java
- Category
- Java
- Title
- get spring application context
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- traversal of list in java
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java
- Title
- java recursion
- Category
- Java
- Title
- java stack
- Category
- Java
- Title
- java null pointer exception
- Category
- Java