在Java

时间:2017-03-31 05:36:15

标签: java java.util.scanner stock cannot-find-symbol

我正在编写一些代码来从文本板读取静态库存号。我跑了它看看有什么问题,我似乎无法修复这个找不到符号错误。

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();

    }

}

2 个答案:

答案 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();

}