java question - force 2 places in int display (02 instead of 2)

dualblade

Supreme [H]ardness
Joined
Nov 19, 2000
Messages
4,180
helps me out for printing columns. is there a setprecision type of operation for integers that inserts 0's if more places are needed? thanks
 

tim_m

i'm so nice
Joined
Feb 10, 2003
Messages
5,540
yeah, java sucks for not having a printf kind of thing, not to mention for not allowing variable arguments, which could possibly allow printf, and also not having default arguments, and not having operator overloading, and its new generics which don't even allow primitive types, and forcing OOP down your gullet, ....

of course, i have to use it for my classes
 

Velox

Gawd
Joined
Dec 13, 2000
Messages
904
p3n said:
printf ? you mean system.out.println ?

No, he means
printf("%2d", intvar);

As opposed to
System.out.println(new DecimalFormat("00").format(intvar));
 

ameoba

Supreme [H]ardness
Joined
Jan 9, 2001
Messages
6,413
In case you haven't noticed, doing any sort of console I/O is kinda ugly in Java - setting up console keyboard input has always disgusted me. This is because it was always intented to be used for GUI apps.

With that said, I have seen a printf() implementation for Java. It really makes life easier in some contexts.
 

HorsePunchKid

[H]ard|Gawd
Joined
Nov 6, 2003
Messages
1,414
tim_m said:
yeah, java sucks for not having a printf kind of thing
This is pretty new, but see here for all of your printf needs. In particular, there is now a printf(String, ...) method in java.io.PrintWriter, which of course is what the ubiquitous System.out and System.err are.


tim_m said:
not to mention for not allowing variable arguments, which could possibly allow printf
Also new, but there is definitely a varargs syntax now.


tim_m said:
and also not having default arguments, and not having operator overloading
These have been intentionally omitted because they are so frequently abused. I'm not saying that's good or bad; that's just the reasoning behind it. Google will turn up plenty of persuasive writings on these topics. I myself do not miss either all that much, but I know plenty of people really hate that -- they don't feel Sun should restrict the programmers just because people misuse the features in other languages. Totally understandable.


tim_m said:
and its new generics which don't even allow primitive types
This is true but terribly misleading. The whole point behind the new autoboxing feature is to make that irrelevant. You just use Integer in your generic, and the primitive will be promoted or demoted as necessary. As an example, it's perfectly reasonable to make a new List<Integer> foo, add something via foo.add(3141), call functions like Collections.sort(foo), and so on. It's totally unnecessary to have primitive types be usable in generics.


tim_m said:
of course, i have to use it for my classes
It sounds like you'd really appreciate a lot of the new features. Perhaps you could take this class as an opportunity to learn more about them. Though if your classes are anything like the ones I took, they won't care much about teaching you this kind of stuff. :(
 
Top