0

I'm using command line arguments for this one:

public class Pg253E4 {
    public static void main(String[]args) {
        try {
            System.out.println(Average.average(args)); // This part is saying I don't have a method in Average called average(String[] argName)
        } catch(NullPointerException e) {
            System.out.println(e.toString());
        } catch(NumberFormatException e) {
            System.out.println(e.toString());
        } finally {
            System.out.println("Nice job");
        }
    }
}

And this is the the other class:

public class Average {
    public static double average(String[] arrayString) throws NullPointerException,NumberFormatException {
        double sum=0;
        double arrayStorage[] = new double[arrayString.length];

        for(int i = 0; i< arrayString.length; i++) {
            arrayStorage[i] = Double.parseDouble(arrayString[i]);
        }
        for(int i = 0; i<arrayString.length; i++) {
            sum += arrayStorage[i];
        }
        return (sum/arrayString.length);
    }
}

I don't understand why a syntax error pops up saying that I haven't declared or defined any method called average(String[] argName) in the Average class, when I clearly did.

hopper
  • 13,060
  • 7
  • 49
  • 53
user2522281
  • 353
  • 1
  • 3
  • 4

2 Answers2

1

Try creating a new object of the Average type in your main class. To do this write the following line on your main method:
Average a = new Average();

And then change your System.out.println(Average.average(args)) for
System.out.println(a.average(args))

JJDA
  • 70
  • 1
  • 1
  • 5
0

If you are not using packages you need to make sure Average is imported into your Pg253E4 class:

import Average; // Assuming "Average" is not in a package.

public class Pg253E4
{
    // The rest of your stuff
}

Probably best to use at least one package though:

package yourpackage;

public class Pg253E4
{
    // The rest of your stuff
}

package yourpackage;

public class Average
{
    // The rest of your stuff
}

If Pg253E4 and Average don't have enough in common to be in the same package then they should both be put in their own package, in which case you would have to import Average into Pg253E4 again:

package yourfirstpackage;

import yoursecondpackage.Average;

public class Pg253E4
{
    // The rest of your stuff
}

package yoursecondpackage;

public class Average
{
    // The rest of your stuff
}
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
  • 1
    they are in the same package(the default package) – user2522281 Jun 30 '13 at 23:55
  • Java has a history of problems with the access of classes and methods when using the default package. Read [here](http://stackoverflow.com/a/283828/288341), [here](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4989710), and [here](http://mindprod.com/jgloss/import.html). It is almost always recommended to use a package, not the default package. Use packages correctly and you will not have to instantiate a class just to access one of its static methods as the answer you have selected has instructed you to do. – ubiquibacon Jul 01 '13 at 04:04