input double java

‮h‬ttps://www.lautturi.com
input double java

To input a double value in Java, you can use the Scanner class or the BufferedReader class.

Here is an example of how to use the Scanner class to input a double value:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        double number = sc.nextDouble();
        System.out.println("You entered: " + number);
        sc.close();
    }
}

In this example, the Scanner class is used to read a line of input from the console. The nextDouble() method is used to read the next token in the input as a double value, and the value is then printed to the console.

Here is an example of how to use the BufferedReader class to input a double value:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a number: ");
        double number = Double.parseDouble(br.readLine());
        System.out.println("You entered: " + number);
        br.close();
    }
}

In this example, the BufferedReader class is used to read a line of input from the console. The readLine() method is used to read the line as a String, and the Double.parseDouble() method is used to parse the String as a double value. The value is then printed to the console.

For more information on the Scanner class and the BufferedReader class in Java, you can refer to the Java documentation and the Java tutorial.

Created Time:2017-11-01 22:29:52  Author:lautturi