Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Convert String to Integer and then subtract the result and add the difference to a list.  RSS feed

 
Ranch Hand
Posts: 69
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This may seem a simple one yet a little tricky.I am trying to convert two seperate String objects into two seperate Integer objects.Then I am trying to find the difference between two numbers .The tricky part is the String starts letters and then an _ and then the number and ends with a file extension.gz.Let me give an example :

Result should be the difference between 00000127 and 00000123 and append the ABCDE_at the beginning and .gz at the end.
.So the expected result in the above case is :

Here are the steps I am thinking of doing
1.extract numeric part by using substring, so the result would be 00000123 and 00000127.
2.Convert the extracted part to a Integer Object.This is the tricky part,because the String will start with 00000 ,so is it ok if i do the Integer.parseInt("00000123 "),because i think 0000 will be converted into octal.
3.Once both the strings are converted into integers,i need to find their difference and for the difference you create a new string object append ABCDE_ into the beginneing and .gz at the end and add it to a list.

I am comfortable doing step 1 ,step 2 and step 3 is where i need help.

Thanks Udaya
 
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

udaya krishna wrote:. . .
1.extract numeric part by using substring, so the result would be 00000123 and 00000127.

That isn't a stage in your process. The stage would be, “extract numeric part.” If you read Winston's FAQ, you will see he says you are conflating the stage with the method to implement that stage. I can think of several other ways to implement the identification of the numeric part. The substring() method will work nicely if you are absolutely sure the lengths of the prefix and the numeric part are constant, otherwise it is error‑prone.

2.Convert the extracted part to a Integer Object. . . . Integer.parseInt("00000123 "),because i think 0000 will be converted into octal.

Have you looked at the details of that method? Does a leading 0 convert the output to octal, as it would in the case of an int literal?
Are all your numbers positive?

3. . . . find their difference

That's the easiest part of the whole process

and for the difference you create a new string object  . . .

"ABCDE_" + difference + ".gz"
But that won't include your leading 0s. Consider String#format() to include the leading 0s.
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

udaya krishna wrote:. . . the expected result in the above case is :. . .

Where does 125 come from in that List? If you are looking for differences, I would expect to see 4 somewhere.
 
udaya krishna
Ranch Hand
Posts: 69
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the quick response.
I am sure that extract part would work fine because the format of the string will always the same(starts with a string,followwed by _,followed by numeric values,followed by .gz),so i think substring part might just work fine.And as you said difference part is easier too.Other steps are the tricky ones.Let me show the code that I have done so far,so that we can figure out where i am going wrong :
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use StringBuffer. Use StringBuilder.
Line 2 is illegible. Don't declare multiple variables on the same line.
Why are you creating a new List in line 5?
That looks as if you had simply thrown a load of code together,  which will be unmanageable.
Try indexOf("_") + 1 and indexOf("."). Both depend on the text having a particular format.
Try passing the text to a Scanner and using one of its methods with names starting find... to identify the numeric part. Again, that depends on the format of the String and might only recognise positive numbers.
Try String.split() with "_" or "\\." or both combined as the regex to split on. Remember you have to escape the full stop.
If you need all the numbers in the range, try this method.
 
udaya krishna
Ranch Hand
Posts: 69
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch.Should have been clearer upfront.I can use Java7.So many updated methods may not be available!And yeah,the format of the code may not be good enough right now because I am working on getting result first.And I am creating a arrayList to store all the filenames between the range.
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

udaya krishna wrote:Ouch.Should have been clearer upfront.I can use Java7. . . .

Why? It is over five years since Java8 came out. The only method I mentioned not in Java7 was the range() method. You can populate your List with a loop in Java7.

the format of the code may not be good enough right now because I am working on getting result first. . . .

Letting your code formatting slip is a very effective way of producing unmaintainable code. It also makes it harder to get a result because you don't see your own mistakes.
I have made all sorts of suggestions. Please try them out.
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using an IDE, formatting your code can be as simple as a few keystrokes.  Then keep your code formatted be entering your code in a formatted way.  See HowToFormatCode (that's link).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!