why this code display nothing ?

A

alexie ivanov

Hi , my name is alexie ivanov and i'm a novice java programmer .. and
my question is : why does the following code displayes nothing .. i
think it must read the args array , but instead of that a blank line
is printed ... so what's wrong with it ?

public class ReadArgs {
public static void main(String args[]){
String output="" ;
for(int counter=0 ; counter<args.length ; counter++)
output+=args[counter]+" " ;
System.out.println(output);
}
}
 
B

Brad BARCLAY

alexie said:
Hi , my name is alexie ivanov and i'm a novice java programmer .. and
my question is : why does the following code displayes nothing .. i
think it must read the args array , but instead of that a blank line
is printed ... so what's wrong with it ?

There is nothing wrong with this code. Are you supplying arguments
when you run it, ie:

java ReadArgs arg1 arg2 arg3

...? If not, then it's printing a blank line because the length of the
args array is 0.

Brad BARCLAY
 
J

John C. Bollinger

alexie said:
Hi , my name is alexie ivanov and i'm a novice java programmer .. and
my question is : why does the following code displayes nothing .. i
think it must read the args array , but instead of that a blank line
is printed ... so what's wrong with it ?

public class ReadArgs {
public static void main(String args[]){
String output="" ;
for(int counter=0 ; counter<args.length ; counter++)
output+=args[counter]+" " ;
System.out.println(output);
}
}


I don't see anything functionally wrong with the code. (The style,
however, is hard on my eyes.) Very likely the problem is with your
expectations. The arguments to a Java program, as executed by Sun's
"java" command-line tool, are everything _after_ the name of the main
class on the command line. This is different from the behavior of a C
program (whose main function has a similar signature) where the first
argument is normally the name under which the program was invoked (on
Unix and Windows, anyway). This is no real loss, because a Java class
always knows its own name.

Does that answer the question?


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top