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

473 views Asked by At

Description

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

Proposed questions

  • Describe the error in greater detail?
  • How are errors like this traced and repaired?
  • I've used google and StackOverflow. Should I be using a different resource to help with errors like this in my beginner programs?

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
4

There are 4 answers

3
ford prefect On BEST ANSWER

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.

  • Example input: >java sayagain
  • Example output:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at sayagain.main(sayagain.java:11)

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.

0
Ian2thedv On

In Java, source code is compiled to class files using javac sayagain.java, which in turn creates a sayagain.class which is essentially what the JVM (Java Virtual Machine) can understand and execute.

To run the sayagain.class that the javac command generated, you'll use java sayagain

You can also pass arguments, seperated by spaces, to the class that you are running, like so:

java sayagain Hello World !

These arguments is passed to the args array which you defined in your main class, which is the method that will be called first. (You could have called it listOfArguments, like below, and it would have made no difference)

To access any object contained within an array, you use []. Number starts at 0 and ends and length-1. Here is an altered example of your code:

public class sayAgain {

//Eventhough there is two say methods, one takes a String parameter, and one takes an Integer parameter, thus they are different 

public static void say(String s){
    System.out.print(s);
}

public static void say(int i) {
    System.out.print(i);
}

public static void main(String[] listOfArguments){

    //First argument, starting at 0 - Hello
    String arg1 = listOfArguments[0];

    //Second argument - World
    String arg2 = listOfArguments[1];

    //Second argument - 3
    String arg3 = listOfArguments[2];

    //arg3 is a integer but is saved as a String, to use arg3 as a integer, it needs to be parsed
    // Integer, is a class, just like your sayAgain class, and parseInt is a method within that class, like your say method
    int count = Integer.parseInt(arg3);

    //Print each argument 
    say(arg1);
    say(arg2);
    say(arg3);

    //Use the converted argument to, to repeatedly call say, and pass it the current number
    for (int t = 0; t < count; t++) {
        //The variable t is created within the for structure and its scope only spans the area between the { }
        say(t);
    }
}
}

Hope this helps :)

2
Sumit Tyagi On

It looks like typographical error.

you have passed arg :- s is missing from the name as the argument you recieved in main method is args.

 sayagain(arg);  

However if you pass 'args' to your method it still give an error as args is an array type and your function is looking an String type. So the correct call for that function would be.

sayagain(args[0]);  

Check the args length before calling this function otherwise it may throw an ArrayIndexOutOfBoundException if parameter not passed.

0
Wolfpack'08 On

My own spin

  • Correct: System.out.print(args[0]);
  • Incorrect: System.out.print(args); //wrong type.
  • Incorrect: System.out.print(args[1]); //wrong index.
  • Incorrect: System.out.print(arg); //not a variable.
  • Incorrect: System.out.print(arg[0]); //typo (see above).