• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Android Studio - Calculator Project - Can't see whats fault

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is a plus operator button for a calculator app. Everytime I press the + sign the app crashes. I don't see any mistakes in this code and don't understand this error.
In short, the plus button doesn't add a "+" sign as long as the last character from the string <calculation> is equal to the "+" sign. Else just add a "+" sign.



The Error I'm getting; java.lang.StringIndexOutOfBoundsException: length=0; index=-1
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the app crashed? With + or without + ?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're getting that error when the "calculation" string has nothing in it.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the String object in that language not have an "endsWith()" method? If it has one, it would be a lot easier to use that than to write your own code.
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does the String object in that language not have an "endsWith()" method? If it has one, it would be a lot easier to use that than to write your own code.



Yes, only when I press the plus button. For now I don't want the other operator buttons to be involved.
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When the app crashed? With + or without + ?



Sorry my previous answer was targeted at the wrong guy. Can't delete comments on here.

Yes, only when I press the plus button. For now I don't want the other operator buttons to be involved.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Removing the Android specific code it becomes this:

I suspect that the error is generated on this line
What if calculation is empty or null? Then you get an error message like the following:

This is pretty close to what Ian was getting. The error message that I'm getting is slightly different because I'm not running this code in an Android environment.
One solution to this is to check to see if indeed calculation does have a value before you try to get the last character from that string.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way you have this line of code

But you should not be using the ==. You should instead use the equals() method like

However given that you are working with the Character class you could do this:

Which bypasses the toString() method and compares a character value to another character value.
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get this error: java.lang.StringIndexOutOfBoundsException: length=0; index=0

I updated the code, and still face the same problem, nothing works.




 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that this may be a better solution:

You say nothing works, do you still get the same error message?
You may have noticed that I added two extra lines to the code for logging.

If you run this, look at the logs and filter on MyCalculator to see what the value of the tvCalculation.getText() actually is.

It is also totally possible that we are looking at the wrong code. Are you sure that this is where the error is happening?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would still try to find out whether an Android String object has an endsWith(String) method, rather than writing all that special-case stuff yourself.
 
ian bilgaen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with endsWith() method is that it only works with suffix and not with single characters.
I didn't know this got more replies so I'm reading them all now.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ian bilgaen wrote:The problem with endsWith() method is that it only works with suffix and not with single characters.



Can you post a link to the documentation which says the parameter for endsWidth has to be a String of length greater than 1? That isn't how it works in Java and I'd be very surprised if Android had such a requirement.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Can you post a link to the documentation which says the parameter for endsWidth has to be a String of length greater than 1?
That isn't how it works in Java and I'd be very surprised if Android had such a requirement.


Android API Doc wrote:endsWith

added in API level 1
boolean endsWith (String suffix)
Tests if this string ends with the specified suffix.

Parameters
suffix String: the suffix.
Returns
boolean true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise.
Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the equals(Object) method.


Android API Doc: String::endsWith

Java API Doc wrote:endsWith
public boolean endsWith(String suffix)
Tests if this string ends with the specified suffix.
Parameters:
suffix - the suffix.
Returns:
true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise.
Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the equals(Object) method.


Java API Doc: String::endsWith
From what I can tell the API documentation for the endWith in method is fairly close to the same between the Java and Android versions.

What if you use the Android Log class and the logcat filter in Android Studio as noted here
https://developer.android.com/studio/debug/am-logcat.html

You could then do something like this:
First thing that you should do is make sure that you are not dealing with a null value.

Then you should check to see if the string variable actually has a value.

Finally you should check to see if the value against possible values e.g. lastChar == '+'

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I more or less expected. So Ian, I don't really see a barrier to using the endsWith() method to see if a String ends with the "+" character. But it seems like you do see a barrier, so... what's that barrier?
 
ian bilgaen
Ranch Hand
Posts: 36
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:That's what I more or less expected. So Ian, I don't really see a barrier to using the endsWith() method to see if a String ends with the "+" character. But it seems like you do see a barrier, so... what's that barrier?



It's working now. @PaulClapham, I didn't fully read the endsWith() method, now I implemented it and it made everything very easy.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic