Not storing expected String from a JTextArea after input is updated
I have an inner class within a calculator that has working buttons. I have a JTextArea
in which button presses will register a number 1-9 then print a new line once an operation is selected. After an operation is selected I have a boolean which will check whether to calculate the result before printing again. I have attempted to split each number and store it by using:
String s = textArea.getText();
String parts = s.split("n|\=|\-|\/|\*|\+");
While I am expecting something along the lines of
100+
100=
200+
I am instead running into:
100+
100=
java.lang.NumberFormatException: empty String
To do these calculations I have used the following snippet of code:
if(s.length() > 0){
if(calc == true){
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && calc == false){
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
Is there a way to store the numbers to be used after each input without running into this error?
edit: The error occurs with the line
Double.parseDouble(parts[counter]);
when removed it causes no errors but still prints normally. What I've noticed through testing with Double.parseDouble(parts[0]); it would work and return the first string input from buttons, but it seems that after this there is no more value being stored into the
String parts = s.split("n|\=|\-|\/|\*|\+");
array, it seems to be stopping there despite the additional inputs fulfilling the delimiter requirements. I am wondering if this is due to an issue with the split method not adding additional inputs into the array.
As requested I am also posted the full code here where relevant
class operands implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
JButton but = null;
String buttonText = "";
//casts button with object
if(obj instanceof JButton)
{
but = (JButton) obj;
}
//sets string of text to button text
if(but != null)
{
buttonText = but.getText();
}
String s = textArea.getText();
//store numbers after each operand
String parts = s.split("n|\=|\-|\/|\*|\+");
if(s.length() > 0)
{
if(calc)
{
//performs calculation and returns result
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && !calc)
{
//updates function to perform calculation on next operand
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
}
java swing events split jtextarea
|
show 1 more comment
I have an inner class within a calculator that has working buttons. I have a JTextArea
in which button presses will register a number 1-9 then print a new line once an operation is selected. After an operation is selected I have a boolean which will check whether to calculate the result before printing again. I have attempted to split each number and store it by using:
String s = textArea.getText();
String parts = s.split("n|\=|\-|\/|\*|\+");
While I am expecting something along the lines of
100+
100=
200+
I am instead running into:
100+
100=
java.lang.NumberFormatException: empty String
To do these calculations I have used the following snippet of code:
if(s.length() > 0){
if(calc == true){
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && calc == false){
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
Is there a way to store the numbers to be used after each input without running into this error?
edit: The error occurs with the line
Double.parseDouble(parts[counter]);
when removed it causes no errors but still prints normally. What I've noticed through testing with Double.parseDouble(parts[0]); it would work and return the first string input from buttons, but it seems that after this there is no more value being stored into the
String parts = s.split("n|\=|\-|\/|\*|\+");
array, it seems to be stopping there despite the additional inputs fulfilling the delimiter requirements. I am wondering if this is due to an issue with the split method not adding additional inputs into the array.
As requested I am also posted the full code here where relevant
class operands implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
JButton but = null;
String buttonText = "";
//casts button with object
if(obj instanceof JButton)
{
but = (JButton) obj;
}
//sets string of text to button text
if(but != null)
{
buttonText = but.getText();
}
String s = textArea.getText();
//store numbers after each operand
String parts = s.split("n|\=|\-|\/|\*|\+");
if(s.length() > 0)
{
if(calc)
{
//performs calculation and returns result
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && !calc)
{
//updates function to perform calculation on next operand
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
}
java swing events split jtextarea
1
The code, you've posted here give us no hint about your problem. Please provide a Minimal, Complete, and Verifiable example, so we can find your mistake and help you. Please also provide the complete stack trace you've got. BTW: have you tried to skip all empty strings and remove leading/trailing spaces? BTW2:if(calc == true)
is equivalent to ìf(calc), and
if(calc == false)` is equivalent toif(!calc)
.
– Sergiy Medvynskyy
Nov 20 '18 at 7:25
Hello, thank you for your input. I have made appropriate changes as well as more detail into my findings. All the variables are initialized locally in the outer class, and this is just an override for an event click to perform intended
– Matthew Wu
Nov 20 '18 at 7:46
Try adding something like this:if(calc && !parts[counter].equals("")) { ... )
.
– DevilsHnd
Nov 20 '18 at 8:15
Hello, thank you for the fix. It seem to have prevented the error from showing, but I am still running into the problem in which the parts[counter] is not updating and adding the inputting int into array. It seems that with the new condition it wouldn't even run the code snippit anymore.
– Matthew Wu
Nov 20 '18 at 8:20
A Calculator usually keeps a running total. You recalculate the total as each operation is applied. There should be no need to parse the entire text in the text area every time a new operation is encountered.
– camickr
Nov 20 '18 at 15:26
|
show 1 more comment
I have an inner class within a calculator that has working buttons. I have a JTextArea
in which button presses will register a number 1-9 then print a new line once an operation is selected. After an operation is selected I have a boolean which will check whether to calculate the result before printing again. I have attempted to split each number and store it by using:
String s = textArea.getText();
String parts = s.split("n|\=|\-|\/|\*|\+");
While I am expecting something along the lines of
100+
100=
200+
I am instead running into:
100+
100=
java.lang.NumberFormatException: empty String
To do these calculations I have used the following snippet of code:
if(s.length() > 0){
if(calc == true){
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && calc == false){
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
Is there a way to store the numbers to be used after each input without running into this error?
edit: The error occurs with the line
Double.parseDouble(parts[counter]);
when removed it causes no errors but still prints normally. What I've noticed through testing with Double.parseDouble(parts[0]); it would work and return the first string input from buttons, but it seems that after this there is no more value being stored into the
String parts = s.split("n|\=|\-|\/|\*|\+");
array, it seems to be stopping there despite the additional inputs fulfilling the delimiter requirements. I am wondering if this is due to an issue with the split method not adding additional inputs into the array.
As requested I am also posted the full code here where relevant
class operands implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
JButton but = null;
String buttonText = "";
//casts button with object
if(obj instanceof JButton)
{
but = (JButton) obj;
}
//sets string of text to button text
if(but != null)
{
buttonText = but.getText();
}
String s = textArea.getText();
//store numbers after each operand
String parts = s.split("n|\=|\-|\/|\*|\+");
if(s.length() > 0)
{
if(calc)
{
//performs calculation and returns result
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && !calc)
{
//updates function to perform calculation on next operand
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
}
java swing events split jtextarea
I have an inner class within a calculator that has working buttons. I have a JTextArea
in which button presses will register a number 1-9 then print a new line once an operation is selected. After an operation is selected I have a boolean which will check whether to calculate the result before printing again. I have attempted to split each number and store it by using:
String s = textArea.getText();
String parts = s.split("n|\=|\-|\/|\*|\+");
While I am expecting something along the lines of
100+
100=
200+
I am instead running into:
100+
100=
java.lang.NumberFormatException: empty String
To do these calculations I have used the following snippet of code:
if(s.length() > 0){
if(calc == true){
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && calc == false){
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
Is there a way to store the numbers to be used after each input without running into this error?
edit: The error occurs with the line
Double.parseDouble(parts[counter]);
when removed it causes no errors but still prints normally. What I've noticed through testing with Double.parseDouble(parts[0]); it would work and return the first string input from buttons, but it seems that after this there is no more value being stored into the
String parts = s.split("n|\=|\-|\/|\*|\+");
array, it seems to be stopping there despite the additional inputs fulfilling the delimiter requirements. I am wondering if this is due to an issue with the split method not adding additional inputs into the array.
As requested I am also posted the full code here where relevant
class operands implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
JButton but = null;
String buttonText = "";
//casts button with object
if(obj instanceof JButton)
{
but = (JButton) obj;
}
//sets string of text to button text
if(but != null)
{
buttonText = but.getText();
}
String s = textArea.getText();
//store numbers after each operand
String parts = s.split("n|\=|\-|\/|\*|\+");
if(s.length() > 0)
{
if(calc)
{
//performs calculation and returns result
textArea.setText(textArea.getText() + "=" + "n");
d2 = Double.parseDouble(parts[counter]);
result = d1 + d2;
textArea.setText(textArea.getText() + result + buttonText + "n");
d1 = d2;
}
if(Character.isDigit(s.charAt(s.length()-1)) && !calc)
{
//updates function to perform calculation on next operand
textArea.setText(textArea.getText() + buttonText + "n");
d1 = Double.parseDouble(parts[counter]);
counter++;
calc = true;
}
}
}
java swing events split jtextarea
java swing events split jtextarea
edited Nov 20 '18 at 7:45
Matthew Wu
asked Nov 20 '18 at 7:13
Matthew WuMatthew Wu
63
63
1
The code, you've posted here give us no hint about your problem. Please provide a Minimal, Complete, and Verifiable example, so we can find your mistake and help you. Please also provide the complete stack trace you've got. BTW: have you tried to skip all empty strings and remove leading/trailing spaces? BTW2:if(calc == true)
is equivalent to ìf(calc), and
if(calc == false)` is equivalent toif(!calc)
.
– Sergiy Medvynskyy
Nov 20 '18 at 7:25
Hello, thank you for your input. I have made appropriate changes as well as more detail into my findings. All the variables are initialized locally in the outer class, and this is just an override for an event click to perform intended
– Matthew Wu
Nov 20 '18 at 7:46
Try adding something like this:if(calc && !parts[counter].equals("")) { ... )
.
– DevilsHnd
Nov 20 '18 at 8:15
Hello, thank you for the fix. It seem to have prevented the error from showing, but I am still running into the problem in which the parts[counter] is not updating and adding the inputting int into array. It seems that with the new condition it wouldn't even run the code snippit anymore.
– Matthew Wu
Nov 20 '18 at 8:20
A Calculator usually keeps a running total. You recalculate the total as each operation is applied. There should be no need to parse the entire text in the text area every time a new operation is encountered.
– camickr
Nov 20 '18 at 15:26
|
show 1 more comment
1
The code, you've posted here give us no hint about your problem. Please provide a Minimal, Complete, and Verifiable example, so we can find your mistake and help you. Please also provide the complete stack trace you've got. BTW: have you tried to skip all empty strings and remove leading/trailing spaces? BTW2:if(calc == true)
is equivalent to ìf(calc), and
if(calc == false)` is equivalent toif(!calc)
.
– Sergiy Medvynskyy
Nov 20 '18 at 7:25
Hello, thank you for your input. I have made appropriate changes as well as more detail into my findings. All the variables are initialized locally in the outer class, and this is just an override for an event click to perform intended
– Matthew Wu
Nov 20 '18 at 7:46
Try adding something like this:if(calc && !parts[counter].equals("")) { ... )
.
– DevilsHnd
Nov 20 '18 at 8:15
Hello, thank you for the fix. It seem to have prevented the error from showing, but I am still running into the problem in which the parts[counter] is not updating and adding the inputting int into array. It seems that with the new condition it wouldn't even run the code snippit anymore.
– Matthew Wu
Nov 20 '18 at 8:20
A Calculator usually keeps a running total. You recalculate the total as each operation is applied. There should be no need to parse the entire text in the text area every time a new operation is encountered.
– camickr
Nov 20 '18 at 15:26
1
1
The code, you've posted here give us no hint about your problem. Please provide a Minimal, Complete, and Verifiable example, so we can find your mistake and help you. Please also provide the complete stack trace you've got. BTW: have you tried to skip all empty strings and remove leading/trailing spaces? BTW2:
if(calc == true)
is equivalent to ìf(calc), and
if(calc == false)` is equivalent to if(!calc)
.– Sergiy Medvynskyy
Nov 20 '18 at 7:25
The code, you've posted here give us no hint about your problem. Please provide a Minimal, Complete, and Verifiable example, so we can find your mistake and help you. Please also provide the complete stack trace you've got. BTW: have you tried to skip all empty strings and remove leading/trailing spaces? BTW2:
if(calc == true)
is equivalent to ìf(calc), and
if(calc == false)` is equivalent to if(!calc)
.– Sergiy Medvynskyy
Nov 20 '18 at 7:25
Hello, thank you for your input. I have made appropriate changes as well as more detail into my findings. All the variables are initialized locally in the outer class, and this is just an override for an event click to perform intended
– Matthew Wu
Nov 20 '18 at 7:46
Hello, thank you for your input. I have made appropriate changes as well as more detail into my findings. All the variables are initialized locally in the outer class, and this is just an override for an event click to perform intended
– Matthew Wu
Nov 20 '18 at 7:46
Try adding something like this:
if(calc && !parts[counter].equals("")) { ... )
.– DevilsHnd
Nov 20 '18 at 8:15
Try adding something like this:
if(calc && !parts[counter].equals("")) { ... )
.– DevilsHnd
Nov 20 '18 at 8:15
Hello, thank you for the fix. It seem to have prevented the error from showing, but I am still running into the problem in which the parts[counter] is not updating and adding the inputting int into array. It seems that with the new condition it wouldn't even run the code snippit anymore.
– Matthew Wu
Nov 20 '18 at 8:20
Hello, thank you for the fix. It seem to have prevented the error from showing, but I am still running into the problem in which the parts[counter] is not updating and adding the inputting int into array. It seems that with the new condition it wouldn't even run the code snippit anymore.
– Matthew Wu
Nov 20 '18 at 8:20
A Calculator usually keeps a running total. You recalculate the total as each operation is applied. There should be no need to parse the entire text in the text area every time a new operation is encountered.
– camickr
Nov 20 '18 at 15:26
A Calculator usually keeps a running total. You recalculate the total as each operation is applied. There should be no need to parse the entire text in the text area every time a new operation is encountered.
– camickr
Nov 20 '18 at 15:26
|
show 1 more comment
1 Answer
1
active
oldest
votes
thanks to all those who tried to help. I have found the issue.
For the string .split() function, I had "n" as one of the delimiters. Doing so seemed to have an issue in storing the number into the string and as a result would store an empty result into the array. Removing newline from the delimiter seemed to have solved the issue.
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%2f53387966%2fnot-storing-expected-string-from-a-jtextarea-after-input-is-updated%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
thanks to all those who tried to help. I have found the issue.
For the string .split() function, I had "n" as one of the delimiters. Doing so seemed to have an issue in storing the number into the string and as a result would store an empty result into the array. Removing newline from the delimiter seemed to have solved the issue.
add a comment |
thanks to all those who tried to help. I have found the issue.
For the string .split() function, I had "n" as one of the delimiters. Doing so seemed to have an issue in storing the number into the string and as a result would store an empty result into the array. Removing newline from the delimiter seemed to have solved the issue.
add a comment |
thanks to all those who tried to help. I have found the issue.
For the string .split() function, I had "n" as one of the delimiters. Doing so seemed to have an issue in storing the number into the string and as a result would store an empty result into the array. Removing newline from the delimiter seemed to have solved the issue.
thanks to all those who tried to help. I have found the issue.
For the string .split() function, I had "n" as one of the delimiters. Doing so seemed to have an issue in storing the number into the string and as a result would store an empty result into the array. Removing newline from the delimiter seemed to have solved the issue.
answered Nov 20 '18 at 17:41
Matthew WuMatthew Wu
63
63
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%2f53387966%2fnot-storing-expected-string-from-a-jtextarea-after-input-is-updated%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
1
The code, you've posted here give us no hint about your problem. Please provide a Minimal, Complete, and Verifiable example, so we can find your mistake and help you. Please also provide the complete stack trace you've got. BTW: have you tried to skip all empty strings and remove leading/trailing spaces? BTW2:
if(calc == true)
is equivalent to ìf(calc), and
if(calc == false)` is equivalent toif(!calc)
.– Sergiy Medvynskyy
Nov 20 '18 at 7:25
Hello, thank you for your input. I have made appropriate changes as well as more detail into my findings. All the variables are initialized locally in the outer class, and this is just an override for an event click to perform intended
– Matthew Wu
Nov 20 '18 at 7:46
Try adding something like this:
if(calc && !parts[counter].equals("")) { ... )
.– DevilsHnd
Nov 20 '18 at 8:15
Hello, thank you for the fix. It seem to have prevented the error from showing, but I am still running into the problem in which the parts[counter] is not updating and adding the inputting int into array. It seems that with the new condition it wouldn't even run the code snippit anymore.
– Matthew Wu
Nov 20 '18 at 8:20
A Calculator usually keeps a running total. You recalculate the total as each operation is applied. There should be no need to parse the entire text in the text area every time a new operation is encountered.
– camickr
Nov 20 '18 at 15:26