Creating a java program to convert decimal to binary?

I am working on a program that displays three options. These three are:

  • Decimal to binary conversion
  • Convert binary to decimal
  • Output.

If the user selects "Exit" in the selection, the system will take the number and say "Goodbye."

This is currently what I have.

import java.util.Scanner; public class binary { public String toBinary(int n) { if (n == 0) { return "0"; } String binary = ""; while (n > 0) { int rem = n % 2; binary = rem + binary; n = n / 2; } return binary; } public static int binaryTodecimal(int i) { int n = 0; for (int pow = 1; i > 0; pow *= 2, i /= 10) n += pow * (i % 10); return n; } public static void main(String[] args) { int answer2 = 0; String answer; final int info = 10; for (int i = 0; i < info; i++) { Scanner kb = new Scanner(System.in); System.out.println("=================================="); System.out.print("Enter your choice: "); answer = kb.next(); if (answer.equalsIgnoreCase("3")) System.exit(0); System.out.print("Enter a number: "); answer2 = kb.nextInt(); binary decimalToBinary = new binary(); String binary = decimalToBinary.toBinary(answer2); if (answer.equals("1")) System.out.println("The 8-bit binary representation is: " + binary); binary bd = new binary(); int n = bd.binaryTodecimal(answer2); if (answer.equals("2")) System.out.println("The decimal representation is: " + answer2); 

I have questions.

  • When I try to convert the number in answers2 to binary or decimal, I cannot figure out how to actually split answer2 for this to work. I was thinking about making a loop, but not sure what else I can do. Could you by any chance tell me what I need to do? I am new to Java, still trying to learn the ropes.

  • When converting decimal to binary, it prints out a 6-bit or another bit, I want it to be specifically 8-bit. How can i fix this? Example:

     Enter your choice: 1 Enter a number: 16 The 8-bit binary representation is: 10000 
+5
source share
3 answers

You can use the switch case to list the actions you need to perform. The switch performs a selection-based action; you can also develop several actions. for better understanding follow this link

I did not change your conversion logic, but the decision-making process and replaced it with a switch

 import java.util.Scanner; public class BinaryToDecimal { public String toBinary(int n) { if (n == 0) { return "0"; } String binary = ""; while (n > 0) { int rem = n % 2; binary = rem + binary; n = n / 2; } return binary; } public int binaryTodecimal(int i) { int n = 0; for (int pow = 1; i > 0; pow *= 2, i /= 10) n += pow * (i % 10); return n; } public static void main(String[] args) { int answer2 = 0; String answer; final int info = 10; for (int i = 0; i < info; i++) { Scanner kb = new Scanner(System.in); System.out.println("=================================="); System.out.print("Enter your choice: "); answer = kb.next(); switch (answer) { case "1": // if the answer is one do this action System.out.print("Enter a number: "); answer2 = kb.nextInt(); BinaryToDecimal decimalToBinary = new BinaryToDecimal(); String binary = decimalToBinary.toBinary(answer2); System.out.println("The 8-bit binary representation is: " + binary); break; // leave the switch case "2": // if answer is 2 do the following actions System.out.print("Enter a number: "); answer2 = kb.nextInt(); BinaryToDecimal bd = new BinaryToDecimal(); int n = bd.binaryTodecimal(answer2); System.out.println("The decimal representation is: " + n); break; // leave the switch case case "3": // when the answer is 3 System.out.println("Goodbye"); System.exit(0); // break; you need not use here because you have an exit call } } } } 

Output

 ================================== Enter your choice: 1 Enter a number: 25 The 8-bit binary representation is: 11001 ================================== Enter your choice: 2 Enter a number: 11001 The decimal representation is: 25 ================================== Enter your choice: 2 Enter a number: 1000000 The decimal representation is: 64 ================================== Enter your choice: 3 Goodbye 

Also: why use static for the binary ToDecimal method, but not for the binary. I did both levels of the method object. it does more since you create an object for selection. But creating an object for each choice is not necessary, you can use the same object created several times, since you do not use any member variables to do your job, see the code below

  BinaryToDecimal decimalToBinary = new BinaryToDecimal(); Scanner kb = new Scanner(System.in); System.out.println("=================================="); System.out.print("Enter your choice: "); answer = kb.next(); switch (answer) { case "1": System.out.print("Enter a number: "); answer2 = kb.nextInt(); String binary = decimalToBinary.toBinary(answer2); System.out.println("The 8-bit binary representation is: " + binary); break; case "2": System.out.print("Enter a number: "); answer2 = kb.nextInt(); int n = decimalToBinary.binaryTodecimal(answer2); System.out.println("The decimal representation is: " + n); break; case "3": System.out.println("Goodbye"); System.exit(0); break; } 
+1
source

1) When I try to convert the number in answers2 to a binary or decimal number, I cannot figure out how to actually divide answer2 into this in order to work. I was thinking about making a loop, but not sure what else I can do. Could you by any chance tell me what I need to do? I am new to Java, still trying to learn the ropes.

What does "how to break the answer2" mean?

to convert from binary string to decimal, I suggest you

 int decimal = Integer.parseInt(binaryString, 2) 

on the other hand you can use

 String binary = Integer.toBinaryString(decimalNumber); 

2) When converting decimal to binary, it prints out 6-bits, I want specifically for 8-bits. How can i fix this?

If you want 8 bits, you can add enough indentation 0 at the beginning of the binary string:

 String binaryString = Integer.toBinaryString(10); for(int i = 8 - binaryString.length(); i > 0; i--) binaryString = "0" + binaryString; 

Of course, this is not the most effective way, but I think it’s good.

0
source

This is how I made this program. It is completely recursive based.

 import java.util.*; class Dec_To_Bin{ public static void main(String args[]){ Scanner in=new Scanner(System.in); System.out.print("Enter the decimal number:>"); int n=in.nextInt(); Dec_To_Bin ob=new Dec_To_Bin(); ob.binconv(n,0); } public void binconv(int n,int c){ if(c>7){ return ; } else{ binconv(n/2,c+1); System.out.print(n%2); } } 

}

0
source

Source: https://habr.com/ru/post/1241285/


All Articles