0
import java.util.*;

public class PersonList{

    public static void arraylist() {

        // create an array list
        ArrayList Employee = new ArrayList();
        // add elements to the array list
        Scanner input = new Scanner (System.in);
        System.out.println("Name: ");
        Employee.add(input.next());
        System.out.println("Year of birth: ");
        Employee.add(input.next());
        System.out.println("");
        Employee.add(input.next());
        System.out.println("");
        Employee.add(input.next());
        System.out.println("");
        Employee.add(input.next());

        System.out.println("Contents of al: " + Employee);

    }
}

I have an arraylist that takes user entered data and I need to store data that has a space into one block.

I haven't added the other things that I will include. I want to be able to put a name such as "John Smith" into the Name, instead of it printing John, Smith in two separate blocks. I am very new to programming and I apologize if it is sloppy and/or annoying.

4
  • 2
    Use input.nextLine() Commented Apr 13, 2016 at 19:49
  • so simple! thank you. Commented Apr 13, 2016 at 19:55
  • Also, call your Employee variable employee to respect naming conventions and get rid of those awful raw types. Commented Apr 13, 2016 at 19:56
  • Thank you for the tip! It has been updated. Commented Apr 13, 2016 at 20:05

1 Answer 1

1

Replace input.next() with input.nextLine(). The former will usually split your input based on whitespaces, whereas the latter will give you the whole line.

On a side note, there is a naming convention for variables, which requires them to start with a lowercase letter. Moreover, it is discouraged to use raw types for lists and it is prudent to always explicitly specify the type. You can change the whole arraylist creation line to something like ArrayList<String> employees = new ArrayList<>();.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.