Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String correction

 
johnny s smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a string "1234567891011121415....100000"
I need to correct it by inserting the missing numbers, like 13 in the above case. The string can be very huge and has no delimiters. How do you think i should go about doing this?
 
Joseph Macer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the numbers are in a specific order like that, you could use an int (or your own logic) to keep track of what number you expect next. Then reading in the next X digits to see if it IS that expected number would be pretty easy.
 
johnny s smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i know this solution but imagine the number of comparisons? i would need to make...coz this sequence could end at a crore or 100 crores or even more. i need something smarter!
[ April 21, 2008: Message edited by: johnnysss smithxxxx ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49771
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, both of you.

Drop Bear? Is that a real name?

But whatever his name, he is probably correct. If you really have a String of concatenated numbers like that, then a linear search is probably the easiest way to insert it.
You will probably have to use the methods of the Integer class like toString(int i) to get the next String to look for; you can use the length of that String to increment a counter variable, so you can work out where to insert the values in a StringBuilder object. Also (if you know there is one number missing) you can have a boolean variable which tells whether you still have anything to insert; when you have inserted it you can stop your iteration. You are unlikely to have a 100-crore number; remember that is several gigaBytes on disc!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Drop Bear? Is that a real name?


You have doubts about Drop Bear being a real name, but not johnnysss smithxxxx
 
Campbell Ritchie
Sheriff
Pie
Posts: 49771
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are so many people called John Smith that there are bound to be collisions and people have to distinguish themselves from previous members with similar namessssssssssssssssssssssssssssssssssssssssss.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"johnnysss smithxxxx and Drop Bear,"

Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!
 
johnny s smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
done!!! :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49771
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by johnny s smith:
done!!! :-)


Well done. I presume that means you have got the insertions into your String working. Please tell everybody how you did it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic