Last active
April 11, 2018 04:23
-
-
Save justudin/e6a6f2c5eb03c9146ba54e1df6ca6081 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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