Question

In: Computer Science

The Double.parseDouble() method requires a String argument, but it fails if the String cannot be converted...

The Double.parseDouble() method requires a String argument, but it fails if the String cannot be converted to a floating-point number. Write an application in which you try accepting a double input from a user and catch a NumberFormatException if one is thrown. The catch block forces the number to 0 and displays Value entered cannot be converted to a floating-point number. Following the catch block, display the number.

import java.util.*;
public class TryToParseDouble {
    public static void main(String[] args) {
        // Write your code here
    }
}

Solutions

Expert Solution

import java.util.Scanner;

public class TryToParseDouble {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       double num;
       try {
           // Take input and parse into double
           System.out.print("Enter a number: ");
           num = Double.parseDouble(scan.nextLine());
       } catch (NumberFormatException e) {
           // Set num to 0 and print error message
           num = 0;
           System.out.println("Value entered cannot be converted to a floating-point number.");
       }
       // Print number
       System.out.println("Value entered: " + num);
   }
}

OUTPUT


Related Solutions

In the absence of oxygen, pyruvate cannot be converted into acetyl-CoA.  It is instead converted to ____...
In the absence of oxygen, pyruvate cannot be converted into acetyl-CoA.  It is instead converted to ____ and must go to the Cori Cycle into glucose to generate energy.  You would notice this during your exercise. lactic acid ketones glucose lactose
A string of 0s and 1s is to be processed and converted to an even-parity string...
A string of 0s and 1s is to be processed and converted to an even-parity string by adding a parity bit to the end of the string.(For an explanation of the use of parity bits, see Example 30 in Chapter 9.) The parity bit is initially 0. When a 0 character is processed, the parity bit remains unchanged. When a 1 character is processed, the parity bit is switched from 0 to 1 or from 1 to 0. Prove that...
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
organizations cannot thrive if management fails to appreciate the voice of employees in decision making ......
organizations cannot thrive if management fails to appreciate the voice of employees in decision making ... what is the meaning
a) An intangible asset is converted into a tangible asset during the operating cycle. cannot be...
a) An intangible asset is converted into a tangible asset during the operating cycle. cannot be classified on the balance sheet because it lacks physical substance. does not have physical substance, yet often is very valuable. is worthless because it has no physical substance. b) The relationship between current assets and current liabilities is important in evaluating a company’s liquidity. profitability. accounting cycle. market value. c) What is the order in which assets are generally listed on a classified balance...
Suppose that your air conditioner fails on Sunday at midnight (t0 = 0), and you cannot...
Suppose that your air conditioner fails on Sunday at midnight (t0 = 0), and you cannot afford to have it repaired until payday at the end of the month. Assume that the outside temperature varies according to the function. A(t)= 80 − 5 cos(π/12t)-5√3sin(π/12t) and that your inside temperature, u(t) obeys Newton’s law of cooling and is governed by the differential equation du/dt=−0.2(u − A(t)) (a) If your indoor temperature when the air conditioner failed was 70◦F, determine the dynamics...
Suppose that your air conditioner fails on Sunday at midnight (t0 = 0), and you cannot...
Suppose that your air conditioner fails on Sunday at midnight (t0 = 0), and you cannot afford to have it repaired until payday at the end of the month. Assume that the outside temperature varies according to the function A(t) = 80 5 cos(π/12)-5√3sin(π/12t) and that your inside temperature, u(t) obeys Newton’s law of cooling and is governed by the differential equation du/dt= -0.2(u-A(t)) (a) If your indoor temperature when the air conditioner failed was 70◦F , determine the dynamics...
QUESTION TWO Justify the claim that “organizations cannot thrive if management fails to appreciate the voice...
QUESTION TWO Justify the claim that “organizations cannot thrive if management fails to appreciate the voice of employees in decision making”. With relevant examples, discuss the elements of employee voice in organizations. Select any organization of your choice and critically examine the stages of employee voice in this organization citing relevant examples.
Iterative method of solving large systems Is there any example for which Jacobi method fails and...
Iterative method of solving large systems Is there any example for which Jacobi method fails and Gauss-Seidel method succeeds?
Why do many organizations have data that cannot be converted to actionable information? What are the...
Why do many organizations have data that cannot be converted to actionable information? What are the hurdles faced by firms that attempt to query transactional databases?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT