尝试使用方法填充用户输入的ArrayList

时间:2016-04-14 02:33:50

标签: java

我试图用用户输入填充我的数组;但是,我无法正常工作。我正在尝试为用户获取他们想要输入的产品数量的int。然后,我想根据他们想要输入的产品数量,使用实际产品(字符串)的更多用户输入来填充数组。这是我的代码:

config.assets.precompile << "movie-search-bar.js"

1 个答案:

答案 0 :(得分:0)

我可能不完全明白你尝试做什么,但我确实看到了一些有趣的内容......请参阅我的评论

public static ArrayList<String> productBuilder(int num) {

    Scanner scnr = new Scanner(System.in);
    String arrayNames = "";
    ArrayList<String> products = new ArrayList();

    for (int i = 0; i <= num; i++) {
        System.out.print("Enter the products: ");
        // I assume here scnr is supposed to read something
        // and assign it to arrayNames?
        products.add(arrayNames);
    }
    return products;
}

public static boolean getOrder(ArrayList<String> products) {

    Scanner scnr = new Scanner(System.in);
    String guess = "";

    // I assume this line is supposed to be after
    //    guess = scnr.nextLine()?
    boolean productName = products.contains(guess); // very confusing line

    System.out.print("Enter a product: ");
    guess = scnr.nextLine();

    if(productName) {
        System.out.println();
        System.out.print("This product has been found.");
        System.out.println();
    }
    else {
        System.out.println();
    }

    return productName;
}