字符串的扫描仪输入未定义?JAVA
该程序提示店主在一天的开始和结束时输入现金金额以及文件名。它应该检查一天结束时的实际现金金额是否等于预期价值 我有一个txt文件,其中每行包含三项:发票号、现金金额和字母p(如果金额已支付),或R(如果收到)。项目之间用空格分隔 我的代码中的这部分有问题字符串的扫描仪输入未定义?JAVA,java,Java,该程序提示店主在一天的开始和结束时输入现金金额以及文件名。它应该检查一天结束时的实际现金金额是否等于预期价值 我有一个txt文件,其中每行包含三项:发票号、现金金额和字母p(如果金额已支付),或R(如果收到)。项目之间用空格分隔 我的代码中的这部分有问题 while (filename.hasNext()) { invoice.add(filename.nextInt()); cash.add(filename.nextDouble()); PR.add(filename
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);
}
}
}
错误:
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)
如果要扫描文件,请尝试使用此构造函数:
/**
* 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()));
}
现在您正在使用此构造函数,它没有任何意义,因为您将作为参数字符串传递:
public Scanner(String source) {
this(new StringReader(source), WHITESPACE_PATTERN);
}
你认为
filename.nextInt()
应该怎么做?你为什么这么认为?因为filename
是一个String
变量。这些方法由扫描仪
实例提供,在您的代码中,变量在@SotiriosDelimanolis中被称为,我希望它能用文本文件文件名
是字符串中的整数填充数组列表,因此,我应该将它们全部更改为中的?创建另一个扫描仪,该扫描仪将从.next()
中的提供的文件中读取(或者如果您想让用户提供可能包含空格的文件或路径,也可以将其更改为中的)。所以您需要类似于Scanner fileScanner=newscanner(pathToFile)代码>并像使用fileScanner.nextInt()
一样使用它。我应该将什么作为类扫描器的参数?你只是把文件源放进去,我试着在那里添加路径,但看起来不一样right@MikeGoedken你说“看起来不对劲”是什么意思?也不要只描述你的代码,展示它以避免误解。public Scanner(?)抛出FileNotFoundException
对于这一行,我应该向类的参数添加什么?在你的例子中,它可能是这样的:Scanner fileScanner=new Scanner(new File(“/Users/MichaelGoedken/Desktop/transactions.txt”);它有效!:):)非常感谢。
public Scanner(String source) {
this(new StringReader(source), WHITESPACE_PATTERN);
}