Danny
Danny

Reputation: 346

Take user input and create a list with two arguments

I want to take users input to create a list of tasks as shown in the next code:

class Task {
    private int start;
    private int finish;

    public Task(int start, int finish) {
        this.start = start;
        this.finish = finish;
    }

    public int getFinish() {
        return finish;
    }

    public int getStart() {
        return start;
    }

    @Override
    public String toString() {
        return "(" + getStart() + ", " + getFinish() + ")";
    }
}

As you can see a Task is composed by two int one for the start and one for the finish.

Then I have a class that select the tasks with certain conditions and return a list of tasks:

class AS {

    public static List<Task> activitySelector(List<Task> tasks) {

        int k = 0;

        Set<Integer> result = new HashSet<>();

        result.add(0);

        Collections.sort(tasks, Comparator.comparingInt(Task::getStart));

        for (int i = 1; i < tasks.size(); i++) {
            if (tasks.get(i).getStart() >= tasks.get(k).getFinish()) {
                result.add(i);
                k = i;
            }
        }

        return result.stream().map(tasks::get).collect(Collectors.toList());

    }

Then I need the user to type in terminal the numbers (start, finish) to create the list of tasks, but I'm stuck on how to get user input and sort each input into the correct way. I know i can declare the list locally like:

 List<Task> tasks = Arrays.asList(new Task(1, 4), new Task(1, 3), 
                                   new Task(2, 5), new Task(3, 7),
                                   new Task(4, 7), new Task(6, 9), 
                                   new Task(7, 8));

But I don't know how to create List<Task> from user input. Any idea on how I can achieve this?

Upvotes: 0

Views: 149

Answers (3)

Joseph Balnt
Joseph Balnt

Reputation: 140

Here's a fast method using BufferedReader:

    List<Task> tasks = new ArrayList<>();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line;
    while (!((line = br.readLine()).isEmpty())){
        StringTokenizer st = new StringTokenizer(line);
        int startInt = Integer.parseInt(st.nextToken());
        int endInt = Integer.parseInt(st.nextToken());
        // Task is the same Task class you provided
        tasks.add(new Task(startInt, endInt));
    }

    br.close();
    System.out.println(tasks);

The user will enter two numbers separated by a space, per line, and to end, they will enter a blank line.

Example Input

4 6
8 9


Example Output

[(4, 5), (6, 7)]

Upvotes: 1

sarveshseri
sarveshseri

Reputation: 13985

public static RuntimeException createException(String nextLine, RuntimeException exception) {
    return new RuntimeException("invalid input line ["+ nextLine + "]; must be comma seperated start and finish times", exception);
}

public static List<Task> getTaskListUserInput() {
    System.out.println("Please enter tasks as comma seperated start and finish times");
    System.out.println("One task each line");
    System.out.println("For example: 12,20");
    System.out.println("Empty line will be treated as end of input");
    System.out.println("-----------------");

    List<Task> taskList = new ArrayList<>();

    Scanner scanner = new Scanner(System.in);

    String nextLine = scanner.nextLine();

    while (!nextLine.isEmpty()) {
        String[] times = nextLine.split(",");
        if (times.length != 2) {
            throw createException(nextLine, null);
        }
        try {
            int start = Integer.parseInt(times[0]);
            int finish = Integer.parseInt(times[1]);

            Task task = new Task(start, finish);

            taskList.add(task);
        } catch (ArrayIndexOutOfBoundsException | NumberFormatException exception) {
            throw createException(nextLine, exception);
        }

        nextLine = scanner.nextLine();
    }

    System.out.println("-----------------");

    return taskList;
}

Usage :

public static void main(String[] args) {
    List<Task> taskList = getTaskListUserInput();
    System.out.println(taskList.size());
}
Please enter tasks as comma seperated start and finish times
One task each line
For example: 12,20
Empty line will be treated as end of input
-----------------
1,4
7,10

-----------------
2

Process finished with exit code 0

Upvotes: 1

Sai Suman Chitturi
Sai Suman Chitturi

Reputation: 402

public static List<Task> readInput() {
    List<Task> tasks = new ArrayList<Task>();
    Scanner sc = new Scanner(System.in);
    int number_of_tasks = sc.nextInt();
    for(int i = 0; i < number_of_tasks; i++) {
        int start = sc.nextInt();
        int finish = sc.nextInt();
        tasks.add(new Task(start, finish));
    }
    return tasks;
}

Why don't you try this?

Upvotes: 1

Related Questions