Results 1 to 4 of 4

Thread: simple Java Code

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    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.

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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

  3. #3
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    Thank You gothic_type
    U get What U pay for.

  4. #4
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    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
  •