SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 2 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 02 Jan 2018 My Price 7.00

program takes in a binary value, protected

Write a program takes in a binary value, protected by a try/catch block, from the console and displays back to the user the decimal value and exits. You can use Integer.parseInt(bin, 2). If input is not base 2, parseInt will throw an error, which the try block will catch and add display a meaningful message to user, then prompt for a binary number again. 

 

Here is the code I have. However it is not displaying the decimal value.

 

import java.util.Scanner;

 

public class Decimal {

 

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String binary;

    int decimal;

 

    while (true) {

      decimal = 0;

 

      // prompt and read the binary value

      System.out.println("Enter a binary string:");

      binary = input.nextLine();

 

      try {

 

        // iterate through character by character and convert to decimal

        for (int i = 0; i < binary.length(); i++) {

          if (binary.charAt(i) != '0' && binary.charAt(i) != '1') {

            throw new IllegalArgumentException("That is not a binary string");

          }

 

          if (binary.charAt(i) == '1') {

            decimal += Math.pow(2, (binary.length() - i - 1));

          }

        }

 

        // display the decimal value

        System.out.println("Binary: " + binary);

        System.out.println("Decimal: " + decimal);

        break;

 

      } catch (IllegalArgumentException ex) {

        System.out.println(ex.getMessage());

        System.out.println("Press enter to try again.");

        input.nextLine();

      }

    }

  }

 

}

Answers

(5)
Status NEW Posted 02 Jan 2018 06:01 AM My Price 7.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)