3

I am receiving below error, when I try to set value on a JProgressBar.

"Optional cannot be converted to Int"

Could someone please advise any workarounds/Solution??

public GUI(){
    initComponents();
    tL = new TasksToDo();
    jProgressBar1.setValue(tL.retrieveTotalHours());// [Where my error occurs]}
}  

And from the TaskToDo Class, Originally I set this to ArrayList but the warnings said needed to switch to Optional:

  public class TasksToDo {

    public static ArrayList<Task> taskList;

    public TasksToDo(){
        taskList = new ArrayList<Task>();
        taskList.add(new Task(0,"Whitepaper", "Write first draft of Whitepaper",  7));
        taskList.add(new Task(1,"Create Database Structure", "Plan required fields and tables",  1));
        taskList.add(new Task(2,"Setup ODBC Connections", "Create the ODBC Connections between SVR1 to DEV-SVR",  2));

    }

    public void addTask (int taskId, String taskTitle, String taskDescription,  int taskHours){}

    public ArrayList<Task> retrieveTask(){
        return taskList;
    }

    public Optional<Integer> retrieveTotalHours(){
    return taskList.stream().map(e -> e.getTaskHours()).reduce(Integer::sum);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
craig157
  • 355
  • 1
  • 3
  • 18

4 Answers4

5

You have to unwrap the optional and grab the value in it like this. Otherwise you can't assign an Optional where int is needed.

tL.retrieveTotalHours().orElse(0);
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
5

An Optional means that the value need not be there. It is basically there to force the caller to explicitly decide what to when a value does not exist. In your case, you can specifify a default value:

jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));

However, your retrieveTotalHours method probably should not return an Optional in the first place. Stream.reduce returns Optional.empty() when the stream is empty, but in your case it probably should return 0 when the list of tasks is empty. So you can do:

public int retrieveTotalHours(){
    return taskList.stream().map(e -> e.getTaskHours()).reduce(0, Integer::sum);
}

(The 0 argument is the identity, which is returned when the stream is empty.)

or even:

public int retrieveTotalHours(){
    return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
}
Hoopje
  • 12,677
  • 8
  • 34
  • 50
1

Well, basically, an Optional<Integer> is not assignment compatible with int.

But Integer is (after unboxing) ... so change:

jProgressBar1.setValue(tL.retrieveTotalHours());

to

jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));

Note that you must provide an integer value when you call setValue. Null or "nothing" is not acceptable.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

If you are only interested in the sum of hours, you don't need the Optional and can make it simpler:

public int retrieveTotalHours()
{
  return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
}
rogerkl
  • 51
  • 2