Chris James
Chris James

Reputation: 1

Sorting and sorting ArrayList data based on condition

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.

Upvotes: 0

Views: 38

Answers (1)

Ivaylo Toskov
Ivaylo Toskov

Reputation: 4021

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<>();.

Upvotes: 1

Related Questions