Answer:
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String numStr = input.nextLine();
- double num;
- try{
- num = Double.parseDouble(numStr);
- }
- catch(NumberFormatException e){
- num = 0;
- System.out.println("Value entered cannot be converted to a floating point number.");
- }
- }
- }
Step-by-step explanation:
The solution code is written in Java.
Firstly, we create a Scanner object and prompt user to input a number (Line 5-6). Next, we create a try -catch block and place the parseDouble inside the try block. If the input is invalid (e.g. "abc"), a NumberFormatException error will be thrown and captured and set the num to 0 and display the error message (Line 11 - 13).