javacompiler-errorscommand-line-argumentsargument-passingsymbol-not-found

Symbol not found in function's argument in main loop?


Description

I have a beginner's-level program. My goal is to pass an argument in at runtime for the first time.

Proposed questions

My code

class sayagain {

    public static void sayagain(String s){

        System.out.print(s);

    }

    public static void main(String[] args){

        sayagain(arg);  

    }

}

Compile error

print2.java:11: error: cannot find symbol
                print2(arg);
                       ^
  symbol:   variable arg
  location: class print2
1 error

Solution

  • Correct

    arg is not defined. Maybe you mean sayagain(args[0]), which will print the first argument in the main method.

    Explanation of string array types-and-indexes

    args is a string array, so to get the first argument you need to access the first element in the array: [0].

    Warning

    You will be given an index out of bounds error if you do not include any arguments when you call the main method.

    Variable plurality

    There's no built-in function to discover arg as the singular of args. Variables may be anything within the specification of the formal language, even asdfsfaeef. It is much better practice to use a descriptive name; therefore, people tend to use plural form when naming arrays.