fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner userInput = new Scanner(System.in);
  13. String binary;
  14. int answer = 0;
  15. System.out.println("Welcome to the Binary to Denary converter!");
  16. System.out.println("Please enter an 8-BIT Binary value:");
  17.  
  18. binary = userInput.next();
  19.  
  20. for (int i = 0; i < binary.length(); i++) {
  21. if (binary.charAt(i) == '1') {
  22. answer += java.lang.Math.pow(2, binary.length() - i - 1);
  23. }
  24. }
  25.  
  26. System.out.println("Your Denary value is:"+answer);
  27. }
  28. }
Success #stdin #stdout 0.17s 321344KB
stdin
10011011
stdout
Welcome to the Binary to Denary converter!
Please enter an 8-BIT Binary value:
Your Denary value is:155