How to solve StringIndexOutOfBoundsException in my case?

Knowledge Facts
public static float getSelectedHeight() { 
  String selectedHeightValue = (String)heightSpin.getSelectedItem(); 
  if (heightSpin.getSelectedItemPosition() == 0 ) { 
    String feets = selectedHeightValue.substring(0,1); 
    String inches = selectedHeightValue.substring(2,4); 

    return (float) (Float.parseFloat(feets) * 0.3048) + (float) (Float.parseFloat(inches) * 0.0254); 

  } else { 
    return Float.parseFloat(selectedHeightValue); 
  } 
}

Error String index out of bound exception i don't know how to solve it.. Please Help me in solving this issue

**java.lang.StringIndexOutOfBoundsException: length=2; regionStart=2; regionLength=2**
denis_lor

The problem might be the lines with selectedHeightValue.substring(...)

To solve it, first of all as a general rumb of thumb, before doing any operation with substring, you should check selectedHeightValue must be valid (so not NULL and not EMPTY).

And then make sure your string size is enough to make a substring of it (so that it does not exceed its size when you try to access a position in the string that doesn't exist).

Exmple: the string "ABC" has size 3. And have positions: 0,1,2. If you try to access the string "ABC" in position 3 and up (4,5,6...) than I guess is gonna throw an error because you want to access something that doesn't exist.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to solve 'undefined is not a function' in my case

From Dev

How to solve my unit test issue in my case

From Dev

How to solve NoMethodError for this case?

From Dev

How to solve the following case?

From Dev

How to solve svn: E195019: Redirect cycle detected, in my case?

From Dev

What is the cause of an StringIndexOutOfBoundsException in my code

From Dev

How to debug in my case

From Dev

how to resolve StringIndexOutOfBoundsException in android

From Dev

Scala Type Erasure on case classes how to solve

From Dev

How to solve optimistic concurrency update case in JPA

From Dev

How to solve the case when issue in Oracle?

From Dev

Scala Type Erasure on case classes how to solve

From Dev

How to use switch case in my case?

From Dev

How to create timer in my case?

From Dev

How to return an object in my case?

From Dev

How to scroll a div in my case?

From Dev

'this' in javascript, how to use it in my case?

From Dev

How to resolve my promise calls in my case

From Dev

How to vertical align my tag in my case

From Dev

How to center my images in my case?

From Dev

How to solve my error LNK2019

From Dev

How to solve SonarQube complaints about my code?

From Java

How to solve CORS problem of my Django API?

From Dev

How to solve CORS problem of my Django API?

From Dev

How to solve SonarQube complaints about my code?

From Dev

How to solve my http request issue?

From Dev

How to solve the unexpected token error in my jsx?

From Dev

How to solve disturbance in my bot in c#?

From Dev

How to solve my query with single for loop?