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










share|improve this question




















  • 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















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










share|improve this question




















  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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....






share|improve this answer























  • 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










  • 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 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',
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
});


}
});














draft saved

draft discarded


















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

























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....






share|improve this answer























  • 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










  • 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















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....






share|improve this answer























  • 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










  • 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













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....






share|improve this answer














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....







share|improve this answer














share|improve this answer



share|improve this answer








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










  • 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


















  • 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










  • 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)