• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Implementing backspace button for calculator  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get a backspace button to work. I get StringIndexOutOfBoundsException when I press it and do not know why. Any help?
The error occurs at line 121.

 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 121 is this one:

Valid indices in the StringBuilder go from 0 to strBuild.length() - 1 (inclusive).

You're trying to delete the character at index: strBuild.length(), which is outside the range of valid indices.

The index of the last character is strBuild.length() - 1, not strBuild.length().
 
Justin Dutch
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply after doing what you said I get StringIndexOutOfBoundsException : String index out of range -1
 
Sheriff
Posts: 23877
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can get that exact exception from that exact line of code. Perhaps something else has changed? Some more detail would help, I think.
 
Justin Dutch
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the updated code where I got the error


 
Paul Clapham
Sheriff
Posts: 23877
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Then your next step is to examine the stack trace (which you didn't share with us) to find out what line of code threw the exception.
 
Justin Dutch
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right I should have posted that, my apologies.
The error occurs at line 128

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at what happens in your actionPerformed method.

Line 108: Here you make the StringBuilder empty.
Line 121: The StringBuilder is still empty, so strBuild.length() is 0, so you try to delete the character at index -1.

You want to set strBuilder to the desired content here, which is the content of the text field without the last character.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Justin Dutch wrote:I am trying to get a backspace button to work.


I'm no GUI expert, so I'm not going to try to advise on specifics like that; but it seems to me that you're mixing up the business of what a calculator does with how you display it. GUI code is all about display, and it tends to be big and cumbersome and full of boilerplate. What a calculator does is much more specific, and can be done without any GUI code at all.

Assuming that you don't want to use RPN (which is actually much simpler), the "mill" of your calculator could be an operator precedence parser (also sometimes called an "infix expression parser"), which is activated whenever you press the '=' button; otherwise, your GUI simply adds characters to a "result" field.

If your "result" field is something like a JTextField, I'm pretty sure that backspace would then work automatically, and you can take all the "calculation guts" out of your GUI and put it in a separate class that simply takes a String expression (or indeed, individual characters) and returns you a result. Personally, I'd be tempted to test this class without a GUI at all and simply supply it with characters via a Scanner (java.util.Scanner).

That way, you separate what your Calculator does from what it looks like.

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!