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
IN JAVA A recursive method that takes a String as its argument and returns a list...
IN JAVA A recursive method that takes a String as its argument and returns a list of Strings which includes all anagrams of the String. This method will contain a loop that generates each possible substring consisting of all but one character in the input String, ie the substring that omits the first letter, then the substring that omits the second letter, etc. Within the loop, the method calls itself recursively for each substring. For each String in the list...
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↵
Write a method in C# which takes string as an argument and extracts all words of...
Write a method in C# which takes string as an argument and extracts all words of length between 4 to 5 and contains vowels in it. Method returns an array of type string containing words which satisfied above criteria. Show these words in main().
Write a recursive method that takes a String argument and recursively prints out each word in...
Write a recursive method that takes a String argument and recursively prints out each word in the String on a different line. Note: You will need to use methods such as indexOf() and substring() within the String class to identify each word and the remaining string. For example, if the input was “the cat purred”, then the method would print the following to the Java console: the cat purred Challenge Problem Write a recursive method that takes a string as...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT