Reading arguments in Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Munk

    Reading arguments in Java

    Hi,

    I have a question.

    Let's say I want to make a simple java program where the first argument
    passed to the java program is doubled and returned. I.e the reult of doing:
    java Double 5 - would return 10.

    This is turning out to be really hard as I have no idea how to "access" this
    argument in java. If someone could help me out that would be really cool.

    Thanks


  • Paul

    #2
    Re: Reading arguments in Java

    Munk wrote:
    [color=blue]
    > Hi,
    >
    > I have a question.
    >
    > Let's say I want to make a simple java program where the first argument
    > passed to the java program is doubled and returned. I.e the reult of doing:
    > java Double 5 - would return 10.
    >
    > This is turning out to be really hard as I have no idea how to "access" this
    > argument in java. If someone could help me out that would be really cool.
    >
    > Thanks
    >
    >[/color]

    args[0] in the main method.

    To change it to an Integer:

    Integer.valueOf (args[0])


    Comment

    • Jonas Kongslund

      #3
      Re: Reading arguments in Java

      Munk wrote:[color=blue]
      > Let's say I want to make a simple java program where the first argument
      > passed to the java program is doubled and returned. I.e the reult of
      > doing: java Double 5 - would return 10.
      >
      > This is turning out to be really hard as I have no idea how to "access"
      > this argument in java. If someone could help me out that would be really
      > cool.[/color]

      The main method's argument contains the command line parameters to your
      program.

      Example:

      public class Hello
      {
      public static void main(String args[]) {
      if (args.length == 1) {
      System.out.prin tln("Hello " + args[0]);
      } else {
      System.out.prin tln("Hello World!");
      }
      }
      }

      --
      Jonas Kongslund

      Comment

      • Rick

        #4
        Re: Reading arguments in Java


        If you're using applets you can use the getParameter function. For example,
        I start appletX with these paremeters

        <html code>
        <APPLET codebase=../.. code="appletX.c lass" width=543 height=297>
        <PARAM NAME = data VALUE = test >
        </APPLET>

        Now you can get that 'data' parameter by calling getParameter( "data" )
        somewhere. Maybe this works with normal Java programs as well.
        But if I'm right you can't name the argments so I doubt if you can
        use it then. In that case, indeed use the main method of your app, like
        Jonas and Paul showed.

        Greetings,
        Rick


        Comment

        • Paul

          #5
          Re: Reading arguments in Java

          Paul wrote:

          [color=blue]
          >
          > Integer.valueOf (args[0])
          >
          >[/color]


          or Integer.parseIn t(args[0]) returns a primitive

          Comment

          Working...