-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary.java
33 lines (18 loc) · 1.11 KB
/
Binary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner; //import
//Date: October 6 2024
//Name: Bob Wang, Binary.java
//Description: Write an application that inputs an integer containing only 0s and 1s (binary) and prints its decimal equivalent.
//Skills: Imports, string manipulation, java syntax, printf statements, parseInt method
public class Binary { //main class
public static void main(String[]args) { //main method
int IntegerEquivalent;
int binary;
String binaryString;
Scanner input=new Scanner(System.in);
System.out.println("Enter a binary number");
binary=input.nextInt(); //Get the user’s input for a binary number and store it in a variable
binaryString=Integer.toString(binary); //Before doing that, convert the input into a string and store it in a another variable
IntegerEquivalent=Integer.parseInt(binaryString,2); //pass the new variable into the integer.parseint method, and store the output in a new integer variable
System.out.printf("This is the integer equivalent of your binary number %d",IntegerEquivalent); //Print the new integer that came out of the user’s binary number
}
}