-
December 10th, 2004, 11:22 AM
#1
Senior Member
simple Java Code
Hello Following is a very simple code for a basically a bigneer like me.
---------------------------------------------------------------------------------------
class FirstProgram
{
public static void main(String h[])
{
System.out.println(h);
}
}
---------------------------------------------------------------------------------------
I have done : javac FirstProgram.java
it is compiling well,
I am giving the input at the runtime to be printed by:
java Firstprogram hello ("hello" is inouted at the run time).
but it not printting "hello", instead giving me the following message:
[Ljava.lang.String;@108786b
I would be really greatfull if someone can educate me, about what is going wrong with the code, and what does this message means.
Regards
Harbir
U get What U pay for.
-
December 10th, 2004, 12:44 PM
#2
The problem is that h is not a String, it's an array of Strings. An array is basically a set of "things" stored together. So you could have int[] a = new int[2]; and that would look like this:
Code:
___ ___
| || |
| || |
--- ---
0 1
The above diagram shows 2 boxes (i.e. an array of two items). To access box one, you would type a[0], and to access box two you would type a[1], so what you need to do in your program is:
Code:
public class FirstClass
{
public static void main(String h[])
{
System.out.println(h[0]); // this accesses the first string stored in h
// i.e. the first command line parameter
}
}
/* so if you type:
java FirstClass one
you would get:
One
as output because one is the first command line parameter after the program name*/
I would advise you to look up arrays on the internet (java arrays specifically) if you didn't understand what I wrote above. Good luck,
Adam
-
December 10th, 2004, 12:52 PM
#3
Senior Member
U get What U pay for.
-
December 10th, 2004, 09:26 PM
#4
It's also an idea to never assume how many elements there are in your array, but rather make sure you just print back everything that's entered on the command line. User h.length for it... and read up on arrays!
/ \\
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|