• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java code for Padding Zero to IP Address : 01.21.03.004 to 001.021.003.004

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dear Sir,

Please tel me java code how to padd zero to IP Address.
For example : i have received ip address as 01.21.03.004 and now i would like pad zero to the received ip address for form full 15 digit ip address as 001.021.003.004
Advice java code for this

Thanks
Rahul
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) split the string into parts according to the dots
2) pad every part
3) lump them together

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Naik wrote:For example : i have received ip address as 01.21.03.004 and now i would like pad zero to the received ip address for form full 15 digit ip address as 001.021.003.004


And just to add to Ivan's good advice, I'd be tempted to convert those 4 numeric strings to actual numbers - and maybe even creating an IPAddress class to hold them - because that's what they are.

As for displaying them: Have a look a String's format() method. It contains all sorts of controls for exactly this kind of stuff.

It should be added that what you're getting looks like an IPv4 address. You might also want to think about what you do if you get an IPv6 one.

Advice java code for this


'Fraid that's not how it works here. ShowSomeEffort (←click) and we'll be happy to help if you run into problems.

Winston
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic