0

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.

wattah
  • 1
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels Feb 04 '17 at 16:35
  • And this has **nothing** to do with the Java compiler by the way and all to do with the way the Scanner behaves when Java is running (the JVM is responsible for running code, not the compiler). – Hovercraft Full Of Eels Feb 04 '17 at 16:35
  • Sorry, I didn’t search accurately. By the way I changed the tag in `java.util.scanner`. Thank you very much! – wattah Feb 04 '17 at 16:53

0 Answers0