How to write double value into JTextField?
I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.
This is a snippet of my code.
String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());
if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}
java swing double jtextfield
add a comment |
I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.
This is a snippet of my code.
String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());
if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}
java swing double jtextfield
Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field
– ControlAltDel
Nov 15 '18 at 16:25
add a comment |
I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.
This is a snippet of my code.
String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());
if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}
java swing double jtextfield
I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.
This is a snippet of my code.
String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());
if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}
java swing double jtextfield
java swing double jtextfield
edited Nov 15 '18 at 23:44
Andrew Thompson
154k28163345
154k28163345
asked Nov 15 '18 at 16:17
bk23bk23
103
103
Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field
– ControlAltDel
Nov 15 '18 at 16:25
add a comment |
Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field
– ControlAltDel
Nov 15 '18 at 16:25
Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field
– ControlAltDel
Nov 15 '18 at 16:25
Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field
– ControlAltDel
Nov 15 '18 at 16:25
add a comment |
3 Answers
3
active
oldest
votes
price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.
Instead you can check for example
if(price != 0.0)
Good answer, but you shouldn't use equals operators on doubles. Math is weird.
– killjoy
Nov 15 '18 at 16:24
add a comment |
Try it:
String tmp = ProductPrice.getText();
if(tmp.isEmpty()) // error
else{
double price = Double.parseDouble(tmp);
// do something
}
add a comment |
As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.
Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.
Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.
So I guess it would be good to handle the possible exception with try catch block like this:
try {
double price = Double.parseDouble(ProductPrice.getText());
} catch (NumberFormatException e) {
// handle error case
}
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%2f53323657%2fhow-to-write-double-value-into-jtextfield%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.
Instead you can check for example
if(price != 0.0)
Good answer, but you shouldn't use equals operators on doubles. Math is weird.
– killjoy
Nov 15 '18 at 16:24
add a comment |
price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.
Instead you can check for example
if(price != 0.0)
Good answer, but you shouldn't use equals operators on doubles. Math is weird.
– killjoy
Nov 15 '18 at 16:24
add a comment |
price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.
Instead you can check for example
if(price != 0.0)
price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.
Instead you can check for example
if(price != 0.0)
answered Nov 15 '18 at 16:19
Nicholas KNicholas K
7,74661537
7,74661537
Good answer, but you shouldn't use equals operators on doubles. Math is weird.
– killjoy
Nov 15 '18 at 16:24
add a comment |
Good answer, but you shouldn't use equals operators on doubles. Math is weird.
– killjoy
Nov 15 '18 at 16:24
Good answer, but you shouldn't use equals operators on doubles. Math is weird.
– killjoy
Nov 15 '18 at 16:24
Good answer, but you shouldn't use equals operators on doubles. Math is weird.
– killjoy
Nov 15 '18 at 16:24
add a comment |
Try it:
String tmp = ProductPrice.getText();
if(tmp.isEmpty()) // error
else{
double price = Double.parseDouble(tmp);
// do something
}
add a comment |
Try it:
String tmp = ProductPrice.getText();
if(tmp.isEmpty()) // error
else{
double price = Double.parseDouble(tmp);
// do something
}
add a comment |
Try it:
String tmp = ProductPrice.getText();
if(tmp.isEmpty()) // error
else{
double price = Double.parseDouble(tmp);
// do something
}
Try it:
String tmp = ProductPrice.getText();
if(tmp.isEmpty()) // error
else{
double price = Double.parseDouble(tmp);
// do something
}
answered Nov 15 '18 at 16:24
TixTix
616
616
add a comment |
add a comment |
As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.
Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.
Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.
So I guess it would be good to handle the possible exception with try catch block like this:
try {
double price = Double.parseDouble(ProductPrice.getText());
} catch (NumberFormatException e) {
// handle error case
}
add a comment |
As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.
Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.
Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.
So I guess it would be good to handle the possible exception with try catch block like this:
try {
double price = Double.parseDouble(ProductPrice.getText());
} catch (NumberFormatException e) {
// handle error case
}
add a comment |
As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.
Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.
Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.
So I guess it would be good to handle the possible exception with try catch block like this:
try {
double price = Double.parseDouble(ProductPrice.getText());
} catch (NumberFormatException e) {
// handle error case
}
As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.
Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.
Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.
So I guess it would be good to handle the possible exception with try catch block like this:
try {
double price = Double.parseDouble(ProductPrice.getText());
} catch (NumberFormatException e) {
// handle error case
}
answered Nov 15 '18 at 16:45
RuslanRuslan
3,426826
3,426826
add a comment |
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%2f53323657%2fhow-to-write-double-value-into-jtextfield%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
Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field
– ControlAltDel
Nov 15 '18 at 16:25