Optional cannot be converted to Int (for use on GUI Progress Bar)
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);
}
}
java netbeans stream optional jprogressbar
add a comment |
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);
}
}
java netbeans stream optional jprogressbar
add a comment |
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);
}
}
java netbeans stream optional jprogressbar
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);
}
}
java netbeans stream optional jprogressbar
java netbeans stream optional jprogressbar
edited Nov 21 '18 at 10:53
Suryavel TR
2,46311420
2,46311420
asked Nov 21 '18 at 9:16
craig157craig157
155
155
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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);
1
That's worked perfectly! Thanks Ravindra
– craig157
Nov 21 '18 at 9:41
add a comment |
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();
}
Thank you for breaking it down Hoopje
– craig157
Nov 21 '18 at 10:38
add a comment |
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.
Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
– craig157
Nov 21 '18 at 9:42
add a comment |
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();
}
Thanks Roger - I will be doing this on another field so will try your method too
– craig157
Nov 21 '18 at 9:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408681%2foptional-integer-cannot-be-converted-to-int-for-use-on-gui-progress-bar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
1
That's worked perfectly! Thanks Ravindra
– craig157
Nov 21 '18 at 9:41
add a comment |
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);
1
That's worked perfectly! Thanks Ravindra
– craig157
Nov 21 '18 at 9:41
add a comment |
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);
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);
answered Nov 21 '18 at 9:21


Ravindra RanwalaRavindra Ranwala
9,34731636
9,34731636
1
That's worked perfectly! Thanks Ravindra
– craig157
Nov 21 '18 at 9:41
add a comment |
1
That's worked perfectly! Thanks Ravindra
– craig157
Nov 21 '18 at 9:41
1
1
That's worked perfectly! Thanks Ravindra
– craig157
Nov 21 '18 at 9:41
That's worked perfectly! Thanks Ravindra
– craig157
Nov 21 '18 at 9:41
add a comment |
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();
}
Thank you for breaking it down Hoopje
– craig157
Nov 21 '18 at 10:38
add a comment |
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();
}
Thank you for breaking it down Hoopje
– craig157
Nov 21 '18 at 10:38
add a comment |
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();
}
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();
}
answered Nov 21 '18 at 9:39
HoopjeHoopje
10k52544
10k52544
Thank you for breaking it down Hoopje
– craig157
Nov 21 '18 at 10:38
add a comment |
Thank you for breaking it down Hoopje
– craig157
Nov 21 '18 at 10:38
Thank you for breaking it down Hoopje
– craig157
Nov 21 '18 at 10:38
Thank you for breaking it down Hoopje
– craig157
Nov 21 '18 at 10:38
add a comment |
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.
Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
– craig157
Nov 21 '18 at 9:42
add a comment |
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.
Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
– craig157
Nov 21 '18 at 9:42
add a comment |
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.
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.
answered Nov 21 '18 at 9:24
Stephen CStephen C
518k70572928
518k70572928
Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
– craig157
Nov 21 '18 at 9:42
add a comment |
Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
– craig157
Nov 21 '18 at 9:42
Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
– craig157
Nov 21 '18 at 9:42
Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
– craig157
Nov 21 '18 at 9:42
add a comment |
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();
}
Thanks Roger - I will be doing this on another field so will try your method too
– craig157
Nov 21 '18 at 9:44
add a comment |
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();
}
Thanks Roger - I will be doing this on another field so will try your method too
– craig157
Nov 21 '18 at 9:44
add a comment |
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();
}
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();
}
answered Nov 21 '18 at 9:40
rogerklrogerkl
312
312
Thanks Roger - I will be doing this on another field so will try your method too
– craig157
Nov 21 '18 at 9:44
add a comment |
Thanks Roger - I will be doing this on another field so will try your method too
– craig157
Nov 21 '18 at 9:44
Thanks Roger - I will be doing this on another field so will try your method too
– craig157
Nov 21 '18 at 9:44
Thanks Roger - I will be doing this on another field so will try your method too
– craig157
Nov 21 '18 at 9:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408681%2foptional-integer-cannot-be-converted-to-int-for-use-on-gui-progress-bar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown