So i've recently started to write code in Java and i have rewritten one of my first programs trying to make it more elegant by using methods. Here is what i came up with:
import java.util.Scanner;
import java.util.ArrayList;
public class groceries {
static Scanner scan = new Scanner(System.in);
static ArrayList<String> shoppinglist = new ArrayList<>();
static int b = 0;
static String n ="";
public static void main(String[] args) {
getAmount(b);
getInput(b, n);
output();
}
private static void message (String m) {
System.out.println(m);
}
private static void getAmount(int b) {
message("How many articles do you want to buy?");
b = scan.nextInt();
}
private static void getInput(int b, String n) {
message("Which articles specifically do you want to buy?");
for (int i = 0; i <= b; i++) {
n = scan.nextLine();
shoppinglist.add(n);
}
}
private static void output() {
message("Here are your purchases:");
for (int k = 0; k<shoppinglist.size(); k++){
message(shoppinglist.get(k));
}
}
}
All works fine until I hit getInput
- I cannot enter any input. The program just wraps up and exits.