0

I don't know what's going on. I should know this by now and have done this dozens of times, but it's not working here.

What I'm trying to do:

I'm trying to pass the itemCode and itemName into commandMethod.(last line)

What I've done:

I've tried different syntax to ensure that I'm not typing it wrong.

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);
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Chairs
  • 1
  • 2
  • 'it is not working' - What error are you seeing when you run? – ganesshkumar Jul 24 '16 at 03:06
  • I'm sorry, I should have been more specific. It's saying that it cannot find either variable. – Chairs Jul 24 '16 at 03:09
  • Also, in my example I have id as a parameter, but i've also tried itemCode. I was trying to solve it by doing something else and that didn't work. – Chairs Jul 24 '16 at 03:10
  • `itemCode` and `itemName` are only *visible* in your `while` loop. Give them more **scope** (move the declarations outside the loop), or invoke the method in the loop. – Elliott Frisch Jul 24 '16 at 03:17
  • If I do that, how will the program store all of the itemNames / itemCodes from the file? – Chairs Jul 24 '16 at 03:21
  • It seems like you have a design problem here. Why are you trying to pass the name and code for a specific item at the same time you pass the entire product array? Why not just use the product array to obtain the item and then call a getter on that item to obtain its name and code? – D.B. Jul 24 '16 at 04:37

0 Answers0