Write a java program please: Asks the user to enter a binary number Validates th
ID: 3588873 • Letter: W
Question
Write a java program please:
Asks the user to enter a binary number
Validates that the entry is a binary number. Nothing but a binary number should be allowed.
The program converts and displays the binary number to its base 10 equivalent, Example 1112 = 710
The user must be asked if he/she wants to continue entering numbers or quit.
Keeps a record in a .txt file named outDataFile.txt with the history of all numbers entered and the associated results, in the following format:
You entered 111
Its base 10 equivalent is 7
You entered 1010101
Its base 10 equivalent is 85
How do I do this without exceptions, methods(only main method), and only import.util.Scanner?
Code:
import java.util.Scanner;
public class Binary {
public static void main(String[] args) {
int binary1, bin;
int deci = 0, num = 0;
boolean userChoice = false;
Scanner input = new Scanner(System.in);
//Loop to ask user to input desired binary number
while(!userChoice)
{
System.out.println("Enter a Binary value:");
binary1 = input.nextInt();
boolean result = true;
if(result == true)
{
bin = binary1;
//Loop converts binary value into the decimal value
do
{
int remain = binary1 % 10;
deci += remain * Math.pow(2, num);
//num = num * 2;
binary1 = binary1 / 10;
num++;
}while(binary1 != 0);
System.out.println("You entered " + bin);
System.out.println("Its base 10 equivalent is: " + deci);
}
else
{
System.out.println("Binary number was not entered.");
}
//Asks if user is still interested in continuing
Scanner input2 = new Scanner(System.in);
System.out.println("Do you wish to continue?(yes or no): ");
String response = input2.nextLine();
if(response.equals("no"))
{
userChoice = true;
}
}
}
}
You entered 111
Its base 10 equivalent is 7
You entered 1010101
Its base 10 equivalent is 85
Explanation / Answer
Hi,
I have fixed the issue and highlighted the code changes below.
Binary.java
import java.util.Scanner;
public class Binary {
public static void main(String[] args) {
int binary1, bin;
int deci = 0, num = 0;
boolean userChoice = false;
Scanner input = new Scanner(System.in);
//Loop to ask user to input desired binary number
while(!userChoice)
{
deci = 0;
num = 0;
System.out.println("Enter a Binary value:");
binary1 = input.nextInt();
boolean result = true;
if(result == true)
{
bin = binary1;
//Loop converts binary value into the decimal value
do
{
int remain = binary1 % 10;
deci += remain * Math.pow(2, num);
//num = num * 2;
binary1 = binary1 / 10;
num++;
}while(binary1 != 0);
System.out.println("You entered " + bin);
System.out.println("Its base 10 equivalent is: " + deci);
}
else
{
System.out.println("Binary number was not entered.");
}
//Asks if user is still interested in continuing
Scanner input2 = new Scanner(System.in);
System.out.println("Do you wish to continue?(yes or no): ");
String response = input2.nextLine();
if(response.equals("no"))
{
userChoice = true;
}
}
}
}
Output:
Enter a Binary value:
111
You entered 111
Its base 10 equivalent is: 7
Do you wish to continue?(yes or no):
yes
Enter a Binary value:
1010101
You entered 1010101
Its base 10 equivalent is: 85
Do you wish to continue?(yes or no):
yes
Enter a Binary value:
111111
You entered 111111
Its base 10 equivalent is: 63
Do you wish to continue?(yes or no):
no
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.