我正在编写一些代码来从文本板读取静态库存号。我跑了它看看有什么问题,我似乎无法修复这个找不到符号错误。
import java.io.*;
import java.util.*;
public class StockMarket
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the filename: ");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
checkValidity();
}
inputFile.close();
}
public static boolean checkValidity()
{
String line = inputFile.nextLine();
double numberOfShares = inputFile.nextDouble();
double buyPricePerShare = inputFile.nextDouble();
double buyComissionRate = inputFile.nextDouble();
double sellPricePerShare = inputFile.nextDouble();
double sellComissionRate = inputFile.nextDouble();
}
}
答案 0 :(得分:2)
您的checkValidity()
方法遇到的问题很少,如下所述:
(1)您的 Scanner
对象范围仅存在于main
方法中,因此将该对象传递给checkValidity
方法
(2)验证checkValidity
方法
(3)您的 checkValidity
方法已return
类型boolean
,因此您需要return
boolean
值从中,即如果验证成功,则返回true
或如果验证失败,则返回false
您可以使用内联注释来引用以下代码:
public static boolean checkValidity(Scanner inputFile) {
String line = inputFile.nextLine();
double numberOfShares = inputFile.nextDouble();
double buyPricePerShare = inputFile.nextDouble();
double buyComissionRate = inputFile.nextDouble();
double sellPricePerShare = inputFile.nextDouble();
double sellComissionRate = inputFile.nextDouble();
boolean validationSuccess = false;
//validate your inputs
// If validations are successful then set validationSuccess = true;
return validationSuccess;//return boolean
}
另外,从scanner
传递main()
对象,如下所示:
while (inputFile.hasNext()) {
checkValidity(inputFile);//pass scanner object
}
答案 1 :(得分:0)
您的方法checkValidity
无效。您尝试访问方法中未知的变量inputFile
。你必须传递参数。
public static boolean checkValidity(Scanner inputFile)
{
String line = inputFile.nextLine();
double numberOfShares = inputFile.nextDouble();
double buyPricePerShare = inputFile.nextDouble();
double buyComissionRate = inputFile.nextDouble();
double sellPricePerShare = inputFile.nextDouble();
double sellComissionRate = inputFile.nextDouble();
}