• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Format the number in java

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
I m writing a java program to get the sequence of number if the from no and to num is given

example
if i give from 1 to 10
the o/p is:
1
2
3
4
5
6
7
8
9
10
this works fine but whn i enter 0001 to 0010
the o/p is same as of the above.
bt i wan to display the value as follows:
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
The sample program as follows:
import java.io.*;

public class Generator{

public static void main(String args[])
{
long arg1=Long.parseLong(args[0]);
long arg2=Long.parseLong(args[1]);


try {
BufferedWriter out = new BufferedWriter(new FileWriter("generate.csv"));
for(long i=arg1;i<=arg2;i++){

out.write("\""+i+"\"");
out.write("\r\n");
}
out.close();
} catch (IOException e) {
}



}
}

any suggestions?

Thanks in advance
Santhosh
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can take the length of the string argument in a integer.
Then just before outputting the value you left pad the result with zeros upto the length of the argument.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sanny,
the following code should solve your problem.
Please, take a look at the comments in the code for
a short explanation.





HTH

Bye
Manio
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering reusability into account you can use a utility method available with StringUtils class with commons-lang-2.0.jar for padding the output to the length of the input string with zero.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution is much simpler than what the others have posted. You just have to change one line. Look at the following line:

out.write("\""+i+"\"");

Modify it to this:

out.write("\"" + String.format("%04d", i) + "\"");

Works if you're using Java 5.0 or newer.
 
Manio Manegra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper, your solution is short and effective, really cool! You enhanced my learning process, thank you.

Sanny, if you want apply this pattern to different padded parameters without the need to change the code you could compose the pattern used to feed String format method at runtime like this:



where totalLenght refers to the first param length (as in the previous posted code)



in this way if you have these arguments:



you should see this output



but, if you change the arguments in:

000001 000010

the output change accordingly (thanks to the run time pattern adjustment):



Bye,
Manio
reply
    Bookmark Topic Watch Topic
  • New Topic