Learn by Example

This section contains a lot of code examples and a small amount of prose. To get the most out of it, run the examples and feel free to experiment and modify them.

Variables & User Input


Variables in Javauto are similar to variables in java. There are eight primitive variable types, but the four most commonly used are String, int, double, and boolean.

// the type must be specified when defining each variable
String str = "This is a string."; // Strings must be enclosed in double quotes
int number = 100;
double pi = 3.1415926535;
boolean b = true;

// since these variables have already been defined we don't have to specify their types
str = "A different string.";
number = number - 50; // subtract 50 from our number
pi = pi * 2; // double pi's value
b = false; // change b's value

// print it all


A different string.

User Input

We can also get user input and store it in a variable.

// get input from command line and print it
String userResponse = input("Enter your name: "); // get user input from the console
String greeting = "Hello " + userResponse + ", it's nice to meet you.";

// get input from a dialog box and display response in a message box
userResponse = inputBox("Enter your name");
greeting = "Hello " + userResponse + ", it's nice to meet you.";
msgBox(greeting, "Hello There");

Type Conversion

Converting between variables isn't hard. You can use toString, toInt, or toDouble to convert any primitive type to one of these three.

String input = input("Enter a number: "); // get their number as a string
int number = toInt(input); // get number as an int
print(input + " + 5 = " + toString(number + 5)); // add five to it and display as string
print(number/2); // divide number by 2 (will return an integer)
print(toDouble(number)/2); // convert number to double and then divide


Enter a number: 5
5 + 5 = 10

String Formatting

For convenience Javauto has some basic syntax that makes string formatting easier.

String name = "John";
String greeting = "Hello %s, I'm %s." % (name, "Javauto");


Hello John, I'm Javauto.

Note that even if we are only replacing one %s the replacement value must be enclosed in ()

print("Hello %s." % ("John"));

This can only be done with string literals, not string variables. Consequently, the following is not valid.

String str = "Hello %s";
print(str % ("John")); // this will NOT work


Operators are identical to Java's operators. These examples briefly summarize them, view this page for complete information.

Mathematical Operators

int a = 10;
int b = 3;

print("10 + 3 = " + (a + b)); // add the two
print("10 x 3 = " + (a * b)); // multiply the two
print("10 / 3 = " + (a / b)); // divide the two, since the result is in int it will be truncated, so instead of 3.333333333 it returns 3
print("10 % 3 = " + (a % b)); // get the remainder. 10 divided by 3 has a remainder of 1


10 + 3 = 13
10 x 3 = 30
10 / 3 = 3
10 % 3 = 1

Logical Operators

int     a = 10;
int     b = 3;
int     c = 3;
boolean bool = true;

print(a > b); // 10 > 3 => true
print(b < a); // 3 < 10 => true
print(b > c); // 3 is not greater than 3 => false
print(b == c); // 3 does equal 3 => true
print(c != a); // 3 does not equal 10 => true
print(b >= c); // 3 is equal to 3, so greate than or equals works => true print(b <= c); // less than will also work => true print(bool); // true is true print(!bool); // !true is the same as not true = false // statements can be grouped in () print( !( (a + b) < c ) ); // evaluates to not (13 < 3). 13 is not less than 3. not (false) => true

// the and operator is &&
print( (b < a) && (c < a) ); // both are true => true
print( (b < a) && (a < c) ); // a is not less than c => false

// the or operator is ||
print( (b < a) || (a < c) ); // as long as one is true or is satisfied => true
print( (!bool) || (a < c) ); // if neither is true => false



Control Flow

Again, anything that works in Java will work in Javauto. For a complete look Java control flow look at this page.

The If Statement

int a = -1;
     if (a > 0) {
	     print( "%s is positive!" % ( toString(a) ) );
     } else if (a < 0) {
	     print( "%s is negative!" % ( toString(a) ) );
     } else {
	     // if it isn't positive or negative it's 0
	     print( "%s is zero" % ( toString(a) ) );

Note that string comparisons must be done with .equals instead of ==

String name = "john";
if (name == "john") { // this compares the two addresses in memory, will not always be true
	print("This will not always happen");
if (name.equals("john")) { // this compares the contents of the string, will equal true
	print("This should always happend");

The While Loop

int i = 0;
while ( i <= 5 ) {
	i = i + 1; // add one to i



The For Loop

for (int i = 0; i <= 5; i++) {


String[] alpha = {"a","b","c","d","e","f"};
for (String letter : alpha) {



User Defined Functions

Void functions

Simple example:

// Define a function to move the mouse to random coordinates.
// This function is void, meaning it doesn't return any value.
func void randomMouseMove() {
	int x = intGetRandom(0, SCREEN_WIDTH); // get a random int for x
	int y = intGetRandom(0, SCREEN_HEIGHT); // get a random int for y
	mouseMove(x,y); // move the mouse to these generated coordinates

msgBox("Moving the mouse to random coordinates...");
randomMouseMove(); // execute the function

More complex example:

// Move the mouse in a sin wave.
// mouseSpeed is the speed to move the mouse at, from 0.00 to 1.00.
// period is the period of the sin wave in arbitrary units. The distance over which one cycle is completed. Range 10-100.
func void doTheWave(double mouseSpeed, double period) {
        setSpeed(mouseSpeed); // set the mouse speed

        int mid = SCREEN_HEIGHT/2;                    // the middle of the screen -- our Y origin
        int amp = SCREEN_HEIGHT/2 - SCREEN_HEIGHT/10; // controls amplitude -- currently covers 8/10 of the screen vertically

        // move the mouse left to right in sine wave
        for (double d = 0.0; d < period; d = d + 3.1415/16) {
                int x = (int) ((d/period) * SCREEN_WIDTH); // x value is the % done times total width
                int y = mid + (int) (Math.sin(d) * amp);  // y value is mid screen plus sin of x multiplied by amplitude
                mouseMove(x,y);                           // move mouse to calculated value

        // move the mouse back across to the left
        for (double d = period; d >= 0; d = d - 3.1415/16) {
                int x = (int) ((d/period) * SCREEN_WIDTH); // x value is the % done times total width
                int y = mid + (int) (Math.sin(d) * amp);  // y value is mid screen plus sin of x multiplied by amplitude
                mouseMove(x,y);                           // move mouse to calculated value

// execute three times
for (int i = 0; i < 3; i++)
        doTheWave(.85, 20);

Functions with a return value

// This function tells us whether the mouse
// is on the left side of the screen.
func boolean isLeft() {
	int xCoord = cursorGetPos()[0]; // get the x mouse position

	if (xCoord < (SCREEN_WIDTH / 2)) { // if we're left of the half way point
		return true;
	} else {
		return false;

if (isLeft()) {
	print("The cursor is on the left side of the screen.");
} else {
	print("The cursor is not on the left side of the screen.");

Global Variables

Global variables are variables that you want to be accessible anywhere in your code. If a variable is not global it will only be available within the scope that it is declared. For instance, a variable declared within a loop or a function will only be available there.

If we run code like this:

String var = "A variable";
func void printVar() {
	print(var); // print our var

We will get this error:

globalTest.ja line 3
error: cannot find symbol
symbol:   variable var
location: class globalTest

The solution is to use the global keyword to make the variable available everywhere.

Make sure you define all your global variables at the beginning of your code and assign them values later on. This is both syntactically correct and organizationally helpful.

global String var; // let the compiler know it's global
var = "A variable";
func void printVar() {
	print(var); // print our var

Now we get:

A variable


A struct is a sort of custom variable.

// define a struct for a computer datatype
// this is easier than creating three parallel arrays of varying types and keeping track of them
struct computer {
	String ID;
	int width;
	int height;

// define a function that can return a variable of type computer
func computer getComputer() {
	computer c = new computer(); // create an instance of this type of variable

	c.ID = USER_NAME.toLowerCase() + "@" + SYSTEM_OS.toLowerCase(); // assign it an ID
	c.width = SCREEN_WIDTH; // set the computer's screen width
	c.height = SCREEN_HEIGHT; // set the computer's screen height

	return c; // return all this data wrapped in our computer datatype

// now we call this function and store the data in "comp"
computer comp = getComputer();

// display its ID
print("ID: %s" % (comp.ID));


ID: john@linux

Or, if we run it with sudo

ID: root@linux


Arrays are the same as in Java. In depth explanation here.

// define two arrays
String[] strArr = {"each", "of", "these", "is", "an", "element"};
int[] intArr =    {0, 1, 1, 2, 3, 5, 8, 13, 21, 34};

print( strArr[0] ); // print the first element of the string array
strArr[0] = "new!"; // change the first element
print( strArr[0] ); // print it again

// we can get the size of the array with .length
int strArrSize = strArr.length; // this will return 6, there are six elements

// print the last element
print( strArr[strArrSize-1] ); // the last element is one less than the length

// print everything in the intArray
for (int i = 0; i < intArr.length; i++) {
	print( toString(i) + "\t" + toString( intArr[i] ) );


Index	Value
0	0
1	1
2	1
3	2
4	3
5	5
6	8
7	13
8	21
9	34

Array Lists

The ArrayList class is borrowed directly from Java and already imported for you.

An ArrayList is useful when you want a more flexible type of array. You can append elements, remove elements, search for elements, and sort with ease. Full documentation on the ArrayList class can be found here.

They are defined like ArrayList<Type> varName = new ArrayList<Type>(); where Type is the variable type of each index. For example to define an ArrayList of strings:

ArrayList<String> strList = new ArrayList<String>();

Some of the most useful ArrayList functions are .add, .remove, .get, .set, .contains, and .size.

/* for example we could add some things to our string list */

// our list now contains ["one", "two", "three", "four"]
// strList.size() now returns 4
// strList.contains("four") is true
// strList.contains("five") is false

/* now we can change around some values */
strList.set(0, "five"); // list is now ["five", "two", "three", "four"]
strList.add(1, "four"); // list is now ["five", "four", "two", "three", "four"]

/* now we can remove some values */
strList.remove("four"); // removes the first instace of "four"
// our list is now ["five", "two", "three", "four"]
strList.remove(0); // remove the first element
// our list is now ["two", "three", "four"]

String Operations

Strings in Javauto are java.lang.String objects. This means that Java string operations should be used when working with strings. There will be a brief overview here, but a more complete explanation of the String class and its methods can be found here.

Method Description Example
.endsWith(String s) Returns true if a string ends with a certain string, or false if it does not.
"foobar".endsWith("bar"); // returns true
.equals(String s) Returns true if a string is equal to a certain string, or false if it is not.
"foobar".equals("foobar"); // returns true
.indexOf(String s) Returns the index of the first occurrence of string s within the string.
"foobar".indexOf("o"); // returns 1
.lastIndexOf(String s) Returns the index of the last occurrence of a string s within the string.
"foobar".lastIndexOf("o"); // returns 2
.length Returns the length of a string in characters.
"foobar".length; // returns 6
.matches(String regex) Returns true if the string matches a certain regular expression, or false if it does not.
"foobar".matches("foo.*"); // returns true
.replace(String old, String new) Replace all occurrences of old within a string with new.
"foobar".replace("foo", "crow"); // returns "crowbar"
.startsWith(String s) Returns true if the string starts with s or false if it does not.
"foobar".startsWith("foo"); // returns true
.substring(int start, int end) Returns the part of the string between start and end.
"foobar".substring(2,4); // returns "ob"
.split(String regex) Returns a string array (String[]) of parts of the string split at the delimiter regex.
"foobar".split("b"); // returns [foo, ar]
.toLowerCase() Returns a lower case version of the string.
"fOoBaR".toLowerCase(); // returns "foobar"
.toUpperCase() Returns an upper case version of the string.
"foobar".toUpperCase(); // returns "FOOBAR"
.trim() Trims trailing white space from the string.
"  foo bar ".trim(); // returns "foo bar"

Command Line Arguments

Command line arguments are stored as a string array in the args variable.

// cmdArgs.javauto
for (String arg : args) {

The below shows output when run in different ways:

john@ubuntu:~$ javauto cmdArgs.javauto
john@ubuntu:~$ ./cmdArgs.jar these are different arguments "this is one"
this is one

Command line arguments can be easily parsed to look for flags with isFlagged and getFlaggedArg.

if (isFlagged(args, "-v")) {
	print("Verbose mode activated.");

if (!isFlagged(args, "-f")) { print("Usage: ./cmdArgs.jar -f file"); } else { print( "File: %s" % (getFlaggedArg(args, "-f")) );
john@ubuntu:~$ javauto cmdArgs.javauto
john@ubuntu:~$ ./cmdArgs.jar
Usage: ./cmdArgs.jar -f file
john@ubuntu:~$ ./cmdArgs.jar -f someFile.txt
File: someFile.txt
john@ubuntu:~$ ./cmdArgs.jar -f someFile.txt -v
Verbose mode activated.
File: someFile.txt

Java Integration

It is important to keep in mind that code can be written in regular Java at almost any time. The only two exceptions are user defined functions and class variables -- writing these in Java will result in an error.

This means that you can use Java classes and import Java libraries, in addition to using Java code.

//both of the below are valid
System.out.println("Printing with Java");
print("Printing with Javauto");