Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 edittext, the first one will have a string if 26 characters. i need to get the fisrt 14 characters in the second edittext. how do i do it.

Java
val=(EditText) findViewById(R.id.editText1);
		gtin=(EditText) findViewById(R.id.editText2);
		String sVal=val.getText().toString();
		sVal=sVal.substring(1, 14);
		
		gtin.setText(sVal);


on compiling i get index out of bound exception
Posted
Comments
Richard MacCutchan 2-Jun-15 6:10am    
You should not call toString on items which are already strings. In some cases it will return something other than the item you are expecting.

1 solution

Check the length first instead of assuming its length is 14 or more.
Java
if(sVal.length() >= 14) 
{
   sVal = sVal.substring(1, 14);
}


Good luck!

ps. are you sure you want to start at 1 instead of the first character in the string (at position 0)?
 
Share this answer
 
v2
Comments
Rahul Ramakrishnan 5-Jun-15 0:42am    
got it done...thanks for your valuable comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900