U
/usr/ceo
Okay, unforeseen circumstances have necessitated that I learn Java.
I'll refrain from providing my opinion of Java and extremely high
overhead compared to Perl, but... whatever.
Okay, well, the #1 thing almost every developer needs to do is produce
output, and my favorite routine in Perl for doing this is:
sub puts { for (@_) { print "$_\n" } } # Modeled after Ruby's puts()
routine (mostly).
In Perl, that means I can:
puts( "This is a single line" );
and it will print that line with a newline automatically appended to
the end. But even better, I can:
puts(
"This is line 1",
"This is line 2",
"This is line 3",
"You get the picture..."
);
And all those lines are printed with no adjustments needed to the
puts() routine in Perl. It also allows me to do things like:
puts( shell( "ls -l /tmp" ) ); # Which isn't the best way of doing
things, but for example...
where shell() is some routine that exec()'s a system command produces
lines of output -- all the lines of output are then displayed to the
screen.
How can I do this in Java? I am assuming I can write a puts() routine
that is overloaded in some way so I can pass a single string or an
array of strings and either way it does what I want. I tried this and
it seems to be proper syntax (inside a class with a main()):
Display.java:
package com.myorg.utils;
pubic class Display {
public static void puts( String[] args ) {
for (int i=0; i<args.length; i++)
__puts( arg );
}
public static void puts( String arg ) {
__puts( arg );
}
private static void __puts( String arg )
{ System.out.println( arg ); }
}
TestPuts.java
import com.myorg.utils.Display;
public class TestPuts {
public static void main( String[] args ) {
puts( "A single string test." );
// This is specifically where I seem to be having trouble
passing a
// String[] array to the puts( String[] args ) call
implementation.
// But maybe the implementation itself in the Display class
isn't
// the way to go?
puts( {
"This is line 1",
"This is line 2",
"This is line 3"
} );
}
private static void puts( String[] args ) { Display.puts( args ); }
private static void puts( String arg ) { Display.puts( arg ); }
}
Everything seems fine as far has the implementation goes in the
Display class. But maybe that's the wrong way to go about it? I
can't believe I have to use a fifty letter package name class method
to print a line to the console. (This is one of the things I hate
about Java, but anyway...)
So... Someone who knows Perl and Java both... Maybe you can help me
out. Or someone who knows Java can at least see what I am attempting
to do. Again, this seems extremely bogus to even have to do. Look
how elegant the Perl solution is... But I should get off my soapbox I
guess and just ask for help. (Can you tell I'm irritated that I have
to learn Java???!!! )
/usr/ceo
I'll refrain from providing my opinion of Java and extremely high
overhead compared to Perl, but... whatever.
Okay, well, the #1 thing almost every developer needs to do is produce
output, and my favorite routine in Perl for doing this is:
sub puts { for (@_) { print "$_\n" } } # Modeled after Ruby's puts()
routine (mostly).
In Perl, that means I can:
puts( "This is a single line" );
and it will print that line with a newline automatically appended to
the end. But even better, I can:
puts(
"This is line 1",
"This is line 2",
"This is line 3",
"You get the picture..."
);
And all those lines are printed with no adjustments needed to the
puts() routine in Perl. It also allows me to do things like:
puts( shell( "ls -l /tmp" ) ); # Which isn't the best way of doing
things, but for example...
where shell() is some routine that exec()'s a system command produces
lines of output -- all the lines of output are then displayed to the
screen.
How can I do this in Java? I am assuming I can write a puts() routine
that is overloaded in some way so I can pass a single string or an
array of strings and either way it does what I want. I tried this and
it seems to be proper syntax (inside a class with a main()):
Display.java:
package com.myorg.utils;
pubic class Display {
public static void puts( String[] args ) {
for (int i=0; i<args.length; i++)
__puts( arg );
}
public static void puts( String arg ) {
__puts( arg );
}
private static void __puts( String arg )
{ System.out.println( arg ); }
}
TestPuts.java
import com.myorg.utils.Display;
public class TestPuts {
public static void main( String[] args ) {
puts( "A single string test." );
// This is specifically where I seem to be having trouble
passing a
// String[] array to the puts( String[] args ) call
implementation.
// But maybe the implementation itself in the Display class
isn't
// the way to go?
puts( {
"This is line 1",
"This is line 2",
"This is line 3"
} );
}
private static void puts( String[] args ) { Display.puts( args ); }
private static void puts( String arg ) { Display.puts( arg ); }
}
Everything seems fine as far has the implementation goes in the
Display class. But maybe that's the wrong way to go about it? I
can't believe I have to use a fifty letter package name class method
to print a line to the console. (This is one of the things I hate
about Java, but anyway...)
So... Someone who knows Perl and Java both... Maybe you can help me
out. Or someone who knows Java can at least see what I am attempting
to do. Again, this seems extremely bogus to even have to do. Look
how elegant the Perl solution is... But I should get off my soapbox I
guess and just ask for help. (Can you tell I'm irritated that I have
to learn Java???!!! )
/usr/ceo