Read double from keyboard in Java

后端 未结 4 538
粉色の甜心
粉色の甜心 2021-01-26 08:50

How can I read a double variable from keyboard in Java? I\'m trying to solve this, but it didn\'t works. It said:

Exception in thread \"main\" java.util.I

相关标签:
4条回答
  • 2021-01-26 09:11

    the code you have written is totally correct ,you should check your input order string double int

    0 讨论(0)
  • 2021-01-26 09:24

    try this:

    import java.io.*;
    import java.util.Scanner;
    
    class DoubleDouble
    {
       public static void main (String[] args)
       {
          double value;
          Scanner scan = new Scanner( System.in );
    
          System.out.print("Enter a double:");
          value = scan.nextDouble();
    
          System.out.println("value: " + value +" twice value: " + 2.0*value );
        }
    }
    

    And be sure, that you type: For example: 3.14 so the dot, should be a point

    0 讨论(0)
  • 2021-01-26 09:27

    Tried to reproduce, had no luck. I think that the problem is with your input. here is what I have tried:

    input:

    s
    
    4.51
    
    2
    

    output:

    String: s
    Double: 4.51
    Int: 2
    

    In your code your first scaner takes string, second: double and third takes int.

    0 讨论(0)
  • 2021-01-26 09:36

    You can also use BufferedReader Class to take Double as input. It can be taken as

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    String s=br.readLine();
    
    Double d=Double.parseDouble(br.readLine());
    
    int i=Integer.parseInt(br.readLine());
    
    0 讨论(0)
提交回复
热议问题