0

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?

10
  • What is user input? How are you collecting it ? Commented Jul 21, 2021 at 3:06
  • I tried with Scanner from terminal but not succeeded Commented Jul 21, 2021 at 3:08
  • What do you mean by not succeeding? Commented Jul 21, 2021 at 3:08
  • I wasn't able to take each int given by user and store each in the form of the Task constructor which is (start, finish) Commented Jul 21, 2021 at 3:11
  • 1
    So, you are saying that you successfully asked for two int from user but then failed to call new Task(startInt, finshInt)? Commented Jul 21, 2021 at 3:13

3 Answers 3

1
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?

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

1 Comment

I was missing this line: tasks.add(new Task(start, finish));. This works perfectly, thank you so much.
1
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

Comments

1

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)]

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.