【问题标题】:How to use nextDouble after nextLine inside a while loop如何在while循环内的nextLine之后使用nextDouble
【发布时间】:2015-02-28 02:42:35
【问题描述】:

我正在尝试读取一个文件并将该文件中的内容写入几个不同的文本文件,但我无法让程序写入任何内容,因为我认为我需要在此处插入一个 nextLine() 但是我不知道在哪里。我是这个网站的新手,也是 java 的新手。请帮忙。

    while (readFile.hasNext())
    {

        m = readFile.nextLine();
        p = readFile.nextLine();
        double cos = readFile.nextDouble();
        double bof = readFile.nextDouble();

        profit = bof - cos;

        if (profit >= bb)
        {
            bold.println(m + "  " + p + "  " + formatter.format(profit));
        }
        else if (profit >= ss && profit <= bb)
        {
            solid.println(m + "  " + p + "  " + formatter.format(profit));
        }
        else if (profit >= eS && profit <= ss)
        {
            odd.println(m + "  " + p + "  " + formatter.format(profit));
        }
        else
        {
            flo.println(m + "  " + p + "  " + formatter.format(profit));
        }
    }

【问题讨论】:

  • 如果什么都没有打印(即没有错误,它只是运行并且什么也不做),那么你的 readFile 是空的或者你的 bold/solid/odd/@987654325 @.println 方法不起作用。
  • 我确保我的 readFile 不为空,并且我知道我的方法有效,因为我删除了 m 和 p,当它们只涉及双精度时,程序运行完美。
  • 等等,程序是真的终止了,还是在等待输入?尝试在while 末尾添加println 语句,看看程序是否到达它。
  • 没有输入。我正在使用 File 读取文本文档并将文本文档中的信息拆分为其他 4 个文本文档。一段时间后我确实有一个 println 来验证信息是否被移动。
  • System.out.println(readFile.useDelimiter("\u0000").next("[^\u0000]*"));展示?如果您简化方法并将每个变量直接输入 System.out,会打印什么?

标签: java java.util.scanner printwriter


【解决方案1】:

你应该在阅读完双打后消耗行尾:

    m = readFile.nextLine();
    p = readFile.nextLine();
    double cos = readFile.nextDouble();
    double bof = readFile.nextDouble();
    readFile.nextLine();

【讨论】:

  • 我也是这么想的,但是当我这样做时,它会在控制台中显示错误,说找不到行。
猜你喜欢
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
相关资源
最近更新 更多