Substring removes 2 characters instead of 1
up vote
1
down vote
favorite
My assignment requires me to have this clear last button which removes the last character in a string. as seen from the code, it SHOULD exclude that last number.
Problem is that when I press it the first time after inputting numbers, it removes 2 and subsequent uses take out 1. any idea how to fix this?
code:
case "Clear Last":
String remove;
remove = text.substring(text.length()-1);
switch (remove) {
case "+":
add = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "-":
subtract = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "*":
multiply = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "/":
divide = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
default:
text = text.substring(0, text.length()-1);
display.setText(text);
break;
}
break;
Example output:
77777 -> Clear Last -> 777 -> Clear Last -> 77 -> 778888 -> Clear Last -> 7788 -> Clear Last -> 778
java string switch-statement substring
|
show 3 more comments
up vote
1
down vote
favorite
My assignment requires me to have this clear last button which removes the last character in a string. as seen from the code, it SHOULD exclude that last number.
Problem is that when I press it the first time after inputting numbers, it removes 2 and subsequent uses take out 1. any idea how to fix this?
code:
case "Clear Last":
String remove;
remove = text.substring(text.length()-1);
switch (remove) {
case "+":
add = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "-":
subtract = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "*":
multiply = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "/":
divide = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
default:
text = text.substring(0, text.length()-1);
display.setText(text);
break;
}
break;
Example output:
77777 -> Clear Last -> 777 -> Clear Last -> 77 -> 778888 -> Clear Last -> 7788 -> Clear Last -> 778
java string switch-statement substring
1
Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 20 at 0:37
it's a java GUI Calculator so it might be a little big. I'll add it in the OP then @shmosel
– John
Nov 20 at 0:44
If it's big, cut it down to just the code necessary to reproduce the problem. You might even discover the problem yourself in the process.
– shmosel
Nov 20 at 0:44
@shmosel done. still pretty big tho. removed it from the package so it should run a bit easier
– John
Nov 20 at 1:00
Why do you have so much duplicated code?
– shmosel
Nov 20 at 1:13
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
My assignment requires me to have this clear last button which removes the last character in a string. as seen from the code, it SHOULD exclude that last number.
Problem is that when I press it the first time after inputting numbers, it removes 2 and subsequent uses take out 1. any idea how to fix this?
code:
case "Clear Last":
String remove;
remove = text.substring(text.length()-1);
switch (remove) {
case "+":
add = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "-":
subtract = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "*":
multiply = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "/":
divide = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
default:
text = text.substring(0, text.length()-1);
display.setText(text);
break;
}
break;
Example output:
77777 -> Clear Last -> 777 -> Clear Last -> 77 -> 778888 -> Clear Last -> 7788 -> Clear Last -> 778
java string switch-statement substring
My assignment requires me to have this clear last button which removes the last character in a string. as seen from the code, it SHOULD exclude that last number.
Problem is that when I press it the first time after inputting numbers, it removes 2 and subsequent uses take out 1. any idea how to fix this?
code:
case "Clear Last":
String remove;
remove = text.substring(text.length()-1);
switch (remove) {
case "+":
add = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "-":
subtract = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "*":
multiply = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
case "/":
divide = false;
text = text.substring(0, text.length() - 1);
display.setText(text);
break;
default:
text = text.substring(0, text.length()-1);
display.setText(text);
break;
}
break;
Example output:
77777 -> Clear Last -> 777 -> Clear Last -> 77 -> 778888 -> Clear Last -> 7788 -> Clear Last -> 778
java string switch-statement substring
java string switch-statement substring
edited Nov 21 at 6:41
asked Nov 20 at 0:33
John
83
83
1
Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 20 at 0:37
it's a java GUI Calculator so it might be a little big. I'll add it in the OP then @shmosel
– John
Nov 20 at 0:44
If it's big, cut it down to just the code necessary to reproduce the problem. You might even discover the problem yourself in the process.
– shmosel
Nov 20 at 0:44
@shmosel done. still pretty big tho. removed it from the package so it should run a bit easier
– John
Nov 20 at 1:00
Why do you have so much duplicated code?
– shmosel
Nov 20 at 1:13
|
show 3 more comments
1
Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 20 at 0:37
it's a java GUI Calculator so it might be a little big. I'll add it in the OP then @shmosel
– John
Nov 20 at 0:44
If it's big, cut it down to just the code necessary to reproduce the problem. You might even discover the problem yourself in the process.
– shmosel
Nov 20 at 0:44
@shmosel done. still pretty big tho. removed it from the package so it should run a bit easier
– John
Nov 20 at 1:00
Why do you have so much duplicated code?
– shmosel
Nov 20 at 1:13
1
1
Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 20 at 0:37
Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 20 at 0:37
it's a java GUI Calculator so it might be a little big. I'll add it in the OP then @shmosel
– John
Nov 20 at 0:44
it's a java GUI Calculator so it might be a little big. I'll add it in the OP then @shmosel
– John
Nov 20 at 0:44
If it's big, cut it down to just the code necessary to reproduce the problem. You might even discover the problem yourself in the process.
– shmosel
Nov 20 at 0:44
If it's big, cut it down to just the code necessary to reproduce the problem. You might even discover the problem yourself in the process.
– shmosel
Nov 20 at 0:44
@shmosel done. still pretty big tho. removed it from the package so it should run a bit easier
– John
Nov 20 at 1:00
@shmosel done. still pretty big tho. removed it from the package so it should run a bit easier
– John
Nov 20 at 1:00
Why do you have so much duplicated code?
– shmosel
Nov 20 at 1:13
Why do you have so much duplicated code?
– shmosel
Nov 20 at 1:13
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The problem is here (and all such snippets):-
text = display.getText();
display.setText(text + "7");
You are updating the text before you update the display. You should interchange them and it should work:-
display.setText(text + "7");
text = display.getText();
Even better, just update the value of text and set display outside switch, something like this:-
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
case ".":
text += e.getActionCommand();
break;
case "=":
break;
case "Clear Last":
text = text.substring(0, (text.length() - 1));
break;
case "Clear All":
text = "";
break;
default:
text = "";
break;
}
display.setText(text);
}
Or improve it even further by getting rid of the switch-case - check if e.getActionCommand() is a digit, then do text += e.getActionCommand();, else if....
Wow. Didn't know that was possible reducing the code down hard like that. It's beautiful. I'll try this out right now.
– John
Nov 20 at 1:43
Hot damn. This worked! Thank you so much! That text += e.getActionCommand(); worked perfectly! Now I'll have to set another switch statement so it does nothing if text is null or it's length is 0 or less.
– John
Nov 20 at 1:57
yourdefaultcase should handle it.. if you want to do it manually, you can writeif (e.getActionCommand() != null && !e.getActionCommand().isEmpty())just before theswitch.. Java is beautiful, isn't it?
– Kartik
Nov 20 at 1:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The problem is here (and all such snippets):-
text = display.getText();
display.setText(text + "7");
You are updating the text before you update the display. You should interchange them and it should work:-
display.setText(text + "7");
text = display.getText();
Even better, just update the value of text and set display outside switch, something like this:-
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
case ".":
text += e.getActionCommand();
break;
case "=":
break;
case "Clear Last":
text = text.substring(0, (text.length() - 1));
break;
case "Clear All":
text = "";
break;
default:
text = "";
break;
}
display.setText(text);
}
Or improve it even further by getting rid of the switch-case - check if e.getActionCommand() is a digit, then do text += e.getActionCommand();, else if....
Wow. Didn't know that was possible reducing the code down hard like that. It's beautiful. I'll try this out right now.
– John
Nov 20 at 1:43
Hot damn. This worked! Thank you so much! That text += e.getActionCommand(); worked perfectly! Now I'll have to set another switch statement so it does nothing if text is null or it's length is 0 or less.
– John
Nov 20 at 1:57
yourdefaultcase should handle it.. if you want to do it manually, you can writeif (e.getActionCommand() != null && !e.getActionCommand().isEmpty())just before theswitch.. Java is beautiful, isn't it?
– Kartik
Nov 20 at 1:59
add a comment |
up vote
1
down vote
accepted
The problem is here (and all such snippets):-
text = display.getText();
display.setText(text + "7");
You are updating the text before you update the display. You should interchange them and it should work:-
display.setText(text + "7");
text = display.getText();
Even better, just update the value of text and set display outside switch, something like this:-
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
case ".":
text += e.getActionCommand();
break;
case "=":
break;
case "Clear Last":
text = text.substring(0, (text.length() - 1));
break;
case "Clear All":
text = "";
break;
default:
text = "";
break;
}
display.setText(text);
}
Or improve it even further by getting rid of the switch-case - check if e.getActionCommand() is a digit, then do text += e.getActionCommand();, else if....
Wow. Didn't know that was possible reducing the code down hard like that. It's beautiful. I'll try this out right now.
– John
Nov 20 at 1:43
Hot damn. This worked! Thank you so much! That text += e.getActionCommand(); worked perfectly! Now I'll have to set another switch statement so it does nothing if text is null or it's length is 0 or less.
– John
Nov 20 at 1:57
yourdefaultcase should handle it.. if you want to do it manually, you can writeif (e.getActionCommand() != null && !e.getActionCommand().isEmpty())just before theswitch.. Java is beautiful, isn't it?
– Kartik
Nov 20 at 1:59
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The problem is here (and all such snippets):-
text = display.getText();
display.setText(text + "7");
You are updating the text before you update the display. You should interchange them and it should work:-
display.setText(text + "7");
text = display.getText();
Even better, just update the value of text and set display outside switch, something like this:-
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
case ".":
text += e.getActionCommand();
break;
case "=":
break;
case "Clear Last":
text = text.substring(0, (text.length() - 1));
break;
case "Clear All":
text = "";
break;
default:
text = "";
break;
}
display.setText(text);
}
Or improve it even further by getting rid of the switch-case - check if e.getActionCommand() is a digit, then do text += e.getActionCommand();, else if....
The problem is here (and all such snippets):-
text = display.getText();
display.setText(text + "7");
You are updating the text before you update the display. You should interchange them and it should work:-
display.setText(text + "7");
text = display.getText();
Even better, just update the value of text and set display outside switch, something like this:-
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
case ".":
text += e.getActionCommand();
break;
case "=":
break;
case "Clear Last":
text = text.substring(0, (text.length() - 1));
break;
case "Clear All":
text = "";
break;
default:
text = "";
break;
}
display.setText(text);
}
Or improve it even further by getting rid of the switch-case - check if e.getActionCommand() is a digit, then do text += e.getActionCommand();, else if....
edited Nov 20 at 1:45
answered Nov 20 at 1:35
Kartik
2,48931331
2,48931331
Wow. Didn't know that was possible reducing the code down hard like that. It's beautiful. I'll try this out right now.
– John
Nov 20 at 1:43
Hot damn. This worked! Thank you so much! That text += e.getActionCommand(); worked perfectly! Now I'll have to set another switch statement so it does nothing if text is null or it's length is 0 or less.
– John
Nov 20 at 1:57
yourdefaultcase should handle it.. if you want to do it manually, you can writeif (e.getActionCommand() != null && !e.getActionCommand().isEmpty())just before theswitch.. Java is beautiful, isn't it?
– Kartik
Nov 20 at 1:59
add a comment |
Wow. Didn't know that was possible reducing the code down hard like that. It's beautiful. I'll try this out right now.
– John
Nov 20 at 1:43
Hot damn. This worked! Thank you so much! That text += e.getActionCommand(); worked perfectly! Now I'll have to set another switch statement so it does nothing if text is null or it's length is 0 or less.
– John
Nov 20 at 1:57
yourdefaultcase should handle it.. if you want to do it manually, you can writeif (e.getActionCommand() != null && !e.getActionCommand().isEmpty())just before theswitch.. Java is beautiful, isn't it?
– Kartik
Nov 20 at 1:59
Wow. Didn't know that was possible reducing the code down hard like that. It's beautiful. I'll try this out right now.
– John
Nov 20 at 1:43
Wow. Didn't know that was possible reducing the code down hard like that. It's beautiful. I'll try this out right now.
– John
Nov 20 at 1:43
Hot damn. This worked! Thank you so much! That text += e.getActionCommand(); worked perfectly! Now I'll have to set another switch statement so it does nothing if text is null or it's length is 0 or less.
– John
Nov 20 at 1:57
Hot damn. This worked! Thank you so much! That text += e.getActionCommand(); worked perfectly! Now I'll have to set another switch statement so it does nothing if text is null or it's length is 0 or less.
– John
Nov 20 at 1:57
your
default case should handle it.. if you want to do it manually, you can write if (e.getActionCommand() != null && !e.getActionCommand().isEmpty()) just before the switch.. Java is beautiful, isn't it?– Kartik
Nov 20 at 1:59
your
default case should handle it.. if you want to do it manually, you can write if (e.getActionCommand() != null && !e.getActionCommand().isEmpty()) just before the switch.. Java is beautiful, isn't it?– Kartik
Nov 20 at 1:59
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53384580%2fsubstring-removes-2-characters-instead-of-1%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
Please provide a Minimal, Complete, and Verifiable example.
– shmosel
Nov 20 at 0:37
it's a java GUI Calculator so it might be a little big. I'll add it in the OP then @shmosel
– John
Nov 20 at 0:44
If it's big, cut it down to just the code necessary to reproduce the problem. You might even discover the problem yourself in the process.
– shmosel
Nov 20 at 0:44
@shmosel done. still pretty big tho. removed it from the package so it should run a bit easier
– John
Nov 20 at 1:00
Why do you have so much duplicated code?
– shmosel
Nov 20 at 1:13