78 452
Assignments Done
Successfully Done
In September 2019

Answer to Question #20708 in AJAX | JavaScript | HTML | PHP for rahilpathan

Question #20708
Write a Java program that will accept an integer containing only 0s and 1s (i.e. a “binary” integer) as input and display its decimal equivalent. If user entered 1001, the program will display 9. If user entered 1211, an error message should be displayed.
Expert's answer
import java.util.Scanner;
public class BinaryNumber {
public static void main(String... args) {
Scanner scanner = newScanner(System.in);
String in = scanner.nextLine();
try {
int value =Integer.parseInt(in, 2);
} catch (NumberFormatExceptionex) {
System.out.print("an invalid value");

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions