扫描仪输入未定义为String? JAVA

时间:2017-09-25 18:50:16

标签: java

此程序会提示店主提供当天开始和结束时的现金金额以及文件名称。它应该检查当天结束时的实际现金数量是否等于预期值。

我有一个txt文件,其中每行包含三个项目:发票号,现金金额,如果金额已付,则为字母P,如果收到,则为R.项目以空格分隔。

我在代码

中遇到了这个问题
while (filename.hasNext()) {
    invoice.add(filename.nextInt());
    cash.add(filename.nextDouble());
    PR.add(filename.next());
}

这是一个语法错误,说我无法使用.nextInt() .nextDouble() .next() hasNext()

以下是完整的源代码:

import java.io.*;
import java.util.*;

/**
 * Code for P11.11.  This program takes in a list of baby names and outputs a list of boys and
 * girls names.
 *
 * @Michael Goedken
 */
public class BalanceTransactions {
    public static void main(String[] args) throws FileNotFoundException {
        // The following lines ask the user to enter the file name and the balances at the beginning and the end
        Scanner in = new Scanner(System.in);

        System.out.print("Please enter the file name for input: ");
        String filename = in.next();

        System.out.print("Please enter the cash amount at the start: ");
        double start = in.nextDouble();

        System.out.print("Please enter the cash amount at the end: ");
        double end = in.nextDouble();

        ArrayList<Integer> invoice = new ArrayList<Integer>();
        ArrayList<Double> cash = new ArrayList<Double>();
        ArrayList<String> PR = new ArrayList<String>();

        while (filename.hasNext()) {
            invoice.add(filename.nextInt());
            cash.add(filename.nextDouble());
            PR.add(filename.next());
        }

        for (int i = 0; i < invoice.size(); i++) {
            if (PR.get(i).equals("P")) {
                start -= cash.get(i);
            }
            if (PR.get(i).equals("R")) {
                start += cash.get(i);
            }
        }

        if (start == end || (double) Math.round(start * 1000000000) / 1000000000 == end) {
            System.out.println("There is the correct amount in the register.");

        } else {
            Double difference = ((double) Math.round(start * 100000) / 100000) - end;

            System.out.println("You are off by: " + difference);
        }
    }
}

编辑:我现在添加了一个新的扫描仪,我收到此错误

Scanner fileScanner = new Scanner("/Users/MichaelGoedken/Desktop/transactions.txt");

      while (fileScanner.hasNext()) 
      {
          invoice.add(fileScanner.nextInt());
          cash.add(fileScanner.nextDouble());
          PR.add(fileScanner.next());
      }

ERROR:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at BalanceTransactions.main(BalanceTransactions.java:34)

1 个答案:

答案 0 :(得分:2)

如果要扫描文件,请尝试使用此构造函数:

/**
 * Constructs a new Scanner that produces values scanned
 * from the specified file. Bytes from the file are converted into
 * characters using the specified charset.
 *
 * @param  source A file to be scanned
 * @throws FileNotFoundException if source is not found
 */
public Scanner(File source) throws FileNotFoundException {
    this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
}

现在你正在使用这个构造函数,它没有任何意义,因为你作为参数传递String:

public Scanner(String source) {
    this(new StringReader(source), WHITESPACE_PATTERN);
}