Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35841
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Unmathematical interface (contract) in Java? |
| Date | 2016-10-19 21:49 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <nu97rr$fs1$2@gioia.aioe.org> (permalink) |
| References | <Java-20161003185250@ram.dialup.fu-berlin.de> <nu82su$90g$1@dont-email.me> |
On 10/19/2016 11:19 AM, Daniel Pitts wrote:
> On 10/3/16 11:01 AM, Stefan Ram wrote:
>> Dr. E. F. Codd wrote about mathematics:
>>
>> "A special concern is that of avoiding the need
>> for special treatment of special cases except when
>> absolutely necessary.".
>>
>> In Java, to print »s« indented by »n« spaces
>> (assuming we are at the start of a new line),
>> one can write:
>>
>> java.lang.System.out.printf
>> ( "%" +( n > 0 ? n : "" )+ "s%s%n", "", s );
>>
>> . Yes, you can write "%2s" or "%1s", but "%0s"
>> is not allowed! Now, how unmathematical is this!
>>
> I mean, its weird to construct a printf format string, so I'm not
> surprised that its troubling to do so...
>
> I imagine you can do something of similar effect without special cases,
> if you give up on the idea of a one-liner...
>
>
> final char[] indent = new char[n];
> java.util.Arrays.fill(indent, ' ');
> java.lang.System.out.println(java.lang.String.valueOf(indent) + s);
>
> I would say its even more clear what the intent of this snippet of code
> is, compared to yours. It is possible its more efficient too, though I
> wouldn't put much weight on that.
public static String genSpaces(int nospaces) {
...
}
and
System.out.println(genSpaces(indent) + s);
would be even more clear IMHO.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: Unmathematical interface (contract) in Java? Daniel Pitts <usenet@coloraura.com> - 2016-10-19 08:19 -0700 Re: Unmathematical interface (contract) in Java? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-19 21:49 -0400
csiph-web