This is my Customers class code:
import java.util.ArrayList;
import java.util.Scanner;
public class Customers {
private ArrayList<String> names = new ArrayList<String>();
private ArrayList<Double> transactions = new ArrayList<Double>();
private Scanner s = new Scanner(System.in);
public void addCustomer() {
System.out.println("ADD A CUSTOMER");
System.out.print("Enter a customer’s full name: ");
String name = s.nextLine(); // Something’s wrong here
names.add(name);
System.out.print("Enter an amount: ");
double amount = s.nextDouble();
transactions.add(amount);
System.out.println(name+"’s account ($"+amount+") created.\n");
}
}
This is my Main code:
public class Main {
public static void main(String[] args) {
Customers customers = new Customers();
customers.addCustomer();
customers.addCustomer(); // Something’s wrong here too
}
}
This is my output:
ADD A CUSTOMER
Enter a customer’s full name: Bob Smith
Enter an amount: 5300.87
Bob Smith’s account ($5300.87) created.
ADD A CUSTOMER
Enter a customer’s full name: Enter an amount: 512.41
’s account ($512.41) created.
Process finished with exit code 0
And this is my question: why Java seems (well, actually it does and acts like I’m pressing Enter
) to skip an instruction (String name = s.nextLine();
) when I call the addCustomer()
method the second time? Thank you.