Skip to content

Instantly share code, notes, and snippets.

@justudin
Last active April 11, 2018 04:23
Show Gist options
  • Save justudin/e6a6f2c5eb03c9146ba54e1df6ca6081 to your computer and use it in GitHub Desktop.
Save justudin/e6a6f2c5eb03c9146ba54e1df6ca6081 to your computer and use it in GitHub Desktop.
import java.io.*;
public class BinaryToDecimal {
public static void main(String[] args) {
//get the input data from user
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Masukan Bilangan Binernya: ");
String str = null;
try {
str = bf.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//parse input data into Long
long num = Long.parseLong(str);
long rem;
while (num > 0) {
rem = num % 10;
num = num / 10;
//check if the input data is binary or not
if (rem != 0 && rem != 1) {
System.out.println("Ini bukan bilangan biner.");
System.out.println("Silahkan Coba lagi");
System.exit(0);
}
}
//parse into integer
int i = Integer.parseInt(str, 2);
//print out the result
System.out.println("Desimalnya: " + i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment