• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get value 10 of ID ITM0010 using substring  RSS feed

 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I retrieve the integer value of last digits of String ID ending with prefix "ITM00"
ITM00 is common prefix.

Sample ID's
ITM002 - 2
ITM0010 - 10
ITM0011 - 11
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different approaches you can take. If the beginning of each String is the same, you can simply use substring and create an int from that.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to get the int value of ITM0010 using substring. But it gives value 1 instead of 10.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you using the substring method? Show us your actual code.
 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This prints "10"



If I add


i get

10
ITM0011

So what makes you think you are only getting a value of '1'?
 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to get the last ID from database table, split and increment integer value of it by one.
When I selected the last digits of ITM0010 I got 1 as 1+0 = 1;
So ItemID of table did not go up after ITM0010. I expected ITM0010 placed after ITM009. But Item Id went after ITM001 leaving ITM009 as last record.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rayan alex wrote:I wanted to get the last ID from database table, split and increment integer value of it by one.
When I selected the last digits of ITM0010 I got 1 as 1+0 = 1;
So ItemID of table did not go up after ITM0010. I expected ITM0010 placed after ITM009. But Item Id went after ITM001 leaving ITM009 as last record.


It's really not clear what you're trying to say here.


If itemId is "ITM0010", then itemID.indexOf("0") is 3 (the first '0', right after the 'M') and the substring() call gives "0010". Integer.parseInt("0010") gives 10. Then in your print statement, you add 1 to 10 and get 11.

(All of which you can--and should--see for yourself by breaking up compound statements into simple ones and printing out all the intermediate results. Always a good habit to have when your code is not doing what you think it should.)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rayan alex wrote:When I selected the last digits of ITM0010 I got 1 as 1+0 = 1;

and again, how do you know this?

The code you posted doesn't print out what you get anywhere. Perhaps it is a display issue. Perhaps it is a rounding issue.

My point is that with what you've given us, WE DON'T KNOW.

So, do what Jeff said. print out stuff at each and every step. Print out the value of num. Print out the String 'second'. Make sure those are REALLY doing what you think they are. If so, look for the problem somewhere else. if not, figure out why they are not.

But from what I've tried, it should work, at least with the string of "ITM0010".

Could it be taht txtitemID has the correct value of 11, but it is getting truncated?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!