从标准输入读取double和string值时出错?

时间:2015-08-11 07:11:34

标签: java java.util.scanner

这是我的代码:

public class Solution {
    public static void main(String[] args) {
        /*
         * Enter your code here. Read input from STDIN.
         * Print output to STDOUT.
         * Your class should be named Solution.
         */
        int num = 0;
        double dou = 0.0;
        String s = null;
        Scanner in = new Scanner(System.in);
        if (in.hasNextInt()) {
            num = in.nextInt();
        }

        Scanner d = new Scanner(System.in);
        if (d.hasNextDouble()) {
            dou = d.nextDouble();
        }

        Scanner str = new Scanner(System.in);
        if (str.hasNextLine()) {
            s = str.nextLine();
        }

        System.out.println("String:" + s);
        System.out.println("Double:" + dou);
        System.out.println("Int:" + num);
    }
}

我收到了这个输出:

  

字符串:空
  双:0.0
  INT:42

但它应该看起来像这样:

  

字符串:欢迎使用Hackerrank Java教程!
  双倍:3.1415
  Int:42

任何人都可以解释一下为什么我为字符串获取null值而为{2}获得0.0

9 个答案:

答案 0 :(得分:2)

//你的答案

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int i = in.nextInt();
        double d = in.nextDouble();
        in.nextLine(); // finishes the previous line
        String s = in.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

答案 1 :(得分:0)

我测试它时工作正常。你如何进入你的浮动? 3.1415或3,1415?根据您的本地情况,将为红色,而另一个则为红色。

如果想要将结果放在一行中:

 String chaine=   String.format("String: %s. Double: %.5f. Int: %d", s,dou,num);
 System.out.println(chaine);

答案 2 :(得分:0)

你不应该使用3个扫描仪,一个就足够了。见Scanner method opened and closed twice

除此之外,当只使用一个时,仍然会出现类似问题的问题: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

答案 3 :(得分:0)

此解决方案有效

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int i = scanner.nextInt();
    double d = scanner.nextDouble();
    scanner.nextLine();
    String s = scanner.nextLine();

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);

    scanner.close();

}

答案 4 :(得分:0)

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int i = scan.nextInt();
        double d = scan.nextDouble();
        String s = scan.next();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

答案 5 :(得分:0)

您可以执行以下操作。对于每个新行扫描器。应调用nextLine()。

Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        String s="";
        Double d=0d;
        if(scan.hasNextLine()){
            scan.nextLine();
            if(scan.hasNextDouble()){
                d=scan.nextDouble();
                scan.nextLine();
                s=scan.nextLine();
            }else{
                s=scan.nextLine();
                scan.nextLine();
                d=scan.nextDouble();
            }

        }

答案 6 :(得分:0)

解决方案

public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);

            int i = Integer.parseInt(scan.nextLine());
            Double d = Double.parseDouble(scan.nextLine());
            String s = scan.nextLine();

            System.out.println("String: " + s);
            System.out.println("Double: " + d);
            System.out.println("Int: " + i);
        }

答案 7 :(得分:-1)

嘿这个错误是因为java没有像C这样的fflush()因此当你在double或整数值之后按Enter时,缓冲区包含输入键,这是空的,因此当你使用nextLine()打印时,它打印该行是因此出现空错误,因此您必须使用sc.nextLine()函数将其调用到下一行,因此将显示正确的输出

答案 8 :(得分:-1)

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int i = scan.nextInt();
    double d = scan.nextDouble();
    String s=scan.next();

     System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);

}