将参数传递给方法

时间:2016-07-24 03:01:34

标签: java methods parameter-passing

我不知道发生了什么事。我现在应该知道这个并且已经完成了几十次,但它不能在这里工作。

我正在尝试做什么:

我正在尝试将itemCode和itemName传递给commandMethod。(最后一行)

我做了什么:

我尝试了不同的语法,以确保我输错了。

public class VendingMachineSimulator {

public void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("product.txt"));

    int i = 0;
    String[] newData = new String[25];
    Product[] product = new Product[9];
    String line;

    while ((line = reader.readLine()) != null) {
        String itemCodeParse = line.substring(0, 2);
        String itemNameParse = line.substring(7, 21);
        String itemQuantityParse = line.substring(23, 26);
        String itemPriceParse = line.substring(31, 36);

        String itemCode = itemCodeParse.trim();
        String itemName = itemNameParse.trim();
        int itemQuantity = Integer.parseInt(itemQuantityParse.trim());
        float itemPrice = Float.parseFloat(itemPriceParse.trim());


        product[i] = new Product(itemCode, itemName, itemQuantity, itemPrice);

        i++;
    }
    try {
        if (reader != null) reader.close();
    } catch (IOException e2) {
            System.out.println("Error Closing In File");
    }

    BufferedReader currReader = new BufferedReader(new FileReader("currency.txt"));

    int j = 0;
    String[] currData = new String[25];
    Currency[] currency = new Currency[5];
    String currLine;

    while ((currLine = currReader.readLine()) != null) {
        String currencyNameParse = currLine.substring(0, 17);
        String currencyValueParse = currLine.substring(21, 27);
        String currencyQuantityParse = currLine.substring(35, 38);

        String currencyName = currencyNameParse.trim();
        int currencyQuantity = Integer.parseInt(currencyQuantityParse.trim());
        float currencyValue = Float.parseFloat(currencyValueParse.trim());

        currency[j] = new Currency(currencyName, currencyQuantity, currencyValue);

        j++;
    }
    try {
        if (reader != null) reader.close();
    } catch (IOException e2) {
        System.out.println("Error Closing In File");
    }

    displayMenu();
    commandMethod(currency, product, id, itemName);

0 个答案:

没有答案