Time for action – Interacting with the user

Now, we're going to create an application that takes your name as a parameter, greets you, and displays the number of characters in your name.

  1. Copy the following code in a file named Main.hx
    class Main
    {
      public function new()
      {
      
      }
    
      public static function main()
      {
       var name = neko.Sys.args()[0];
       trace("Hello " + name);
       trace("Your name is " + Std.string(name.length) + " characters long.");
      }
    }
  2. Open a terminal; go to the directory where you saved Main.hx and type the haxe –main Main -neko characterCounter.n command.
  3. Type the command (this is an example with my first name):

    neko characterCounter.n Benjamin

  4. You will see the following output:

    Main.hx:12: Hello Benjamin

    Main.hx:13: Your name is 8 characters long. ...

Get haXe 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.