Hey everyone.

I was doing a simple test program today that my professor is asking us to do just to test to see if his Ant script works correctly and I came across a problem that is confusing me. My prof wants us (after created all of our homework class files) to create a TestProgram class where, basically like junit, tests our programs with his given input values. Well, I have never worked with args before (I know, depressing) but I gave it a ashot and got this error.

TestProgram.java:16: main(java.lang.String[]) in Sum cannot be applied to (java.lang.String)
    program.main(arg[0]);
           ^
TestProgram.java:17: main(java.lang.String[]) in Sum cannot be applied to (java.lang.String)
    program.main(arg[1]);
           ^
TestProgram.java:18: main(java.lang.String[]) in Sum cannot be applied to (java.lang.String)
    program.main(arg[2]);
           ^
TestProgram.java:19: main(java.lang.String[]) in Sum cannot be applied to (java.lang.String)
    program.main(arg[3]);
           ^

Obviously I know exactly what the error is — applying a String array to a String. But I don't see where I am exactly doing that.

Here are my two files:

TestProgram.java

public class TestProgram {

  public static void main(String[] args) {

    // Sum.java Test
    {
    Sum program = new Sum();

    System.out.println("\n" + "Start Sum Test by Matthew Porter");
    String [] arg =  new String[4];
    arg[0]="5";
    arg[1]="7";
    arg[2]="10";
    arg[3]="12";

    program.main(arg[0]);
    program.main(arg[1]);
    program.main(arg[2]);
    program.main(arg[3]);

    System.out.println("End of Craps Test" + "\n");
    }

  }

}

Sum.java

public class Sum {

    public static void main (String[] args) {

        int sumStop = Integer.parseInt(args[0]);
        int sum = 0;

        for (int i=1; i<=sumStop; i++) {
            sum = sum + i;
        }

        System.out.println("Sum: " + sum);

    }

}

Would anyone be so kind to hint at what I am doing wrong?

Dani AI

Generated

Short clarification and a small refactor that makes testing and reuse cleaner.

As pointed out, the compiler error came from passing a single String when Sum.main expects a String[]; calling main with the full array fixes that. For a more robust approach, avoid invoking another class's main for unit testing. Make the logic testable by extracting the computation into a method that returns a value, then call that from a thin main or from your test harness.

Example (refactored Sum with a reusable method):

public class Sum {
  public static int sumUpTo(int n) {
    int total = 0;
    for (int i = 1; i <= n; i++) {
      total += i;
    }
    return total;
  }
}

Example test harness that calls the reusable method (clean, easy to assert or adapt to JUnit):

public class TestProgram {
  public static void main(String[] args) {
    int[] tests = {5, 7, 10, 12};
    for (int t : tests) {
      System.out.println(t + " -> " + Sum.sumUpTo(t));
    }
  }
}

Extra tips and cautions:

  • If you must accept String[] from the command line, convert inside a try/catch to handle NumberFormatException and skip/report bad inputs.
  • Call static methods by class name (Sum.sumUpTo(...)) rather than via an instance; calling a static method on an object reference compiles but is poor style.
  • Keep a lightweight main as an adapter: parse args, validate, then call the pure function; that makes it trivial to replace the harness with JUnit later.
  • For very large n, consider using long for the accumulator (or the arithmetic formula) to avoid overflow.

These small changes make the code clearer, safer, and much easier to test than repeatedly calling another class's main.

Recommended Answers

All 4 Replies

The following lines are causing compilation errors:

// lines 16-19
program.main(arg[0]); // line 16
program.main(arg[1]); // line 17
program.main(arg[2]); // line 18
program.main(arg[3]); // line 19

You declared arg as follows:

String[] arg = new String[4]; // arg is a reference to a String array

On lines 16-19 you index the array referred by arg, thus you get back a single element of type String.

You declared program as follows:

Sum program = new Sum();

On lines 16-19 you invoke the main method defined in class Sum.

Its signature looks like this:

public static void main(String[] args)

As you can see from the method signature, the parameter type args of that main is of type String[], but on lines 16-19 you passed in arguments that were of type String, this is causing the compiler error.

Ok that makes sense. So I fixed that problem and I changed lines 16-19 to program.main(arg); I changed up some more of my code and this is my final code. If you could just skim over it and make sure how im doing it makes sense that would be great.

TestProgram.java

public class TestProgram {

  public static void main(String[] args) {

    // Sum.java Test
    {
    Sum program = new Sum();

    System.out.println("\n" + "Start Sum Test by Matthew Porter");
    String[] arg =  new String[4];
    arg[0]="5";
    arg[1]="7";
    arg[2]="10";
    arg[3]="12";

    program.main(arg);

    System.out.println("End of Sum Test" + "\n");
    }

  }

}

Sum.java

public class Sum {

    public static void main (String[] args) {

        for (int i=0; i<args.length; i++) {
            int sumStop = Integer.parseInt(args[i]);
            summation(sumStop);
        }

    }

    public static void summation (int sumStop) {

        int sum = 0;

        for (int i=1; i<=sumStop; i++) {
            sum = sum + i;
        }

        System.out.println("Sum: " + sum);

    }

}

The following lines (lines 10-14 of TestProgram.java):

String[] arg = new String[4];
arg[0]="5";
arg[1]="7";
arg[2]="10";
arg[3]="12";

Can be compacted to one line as follows:

String[] arg = {"5", "7", "10", "12"};

In theory you could rewrite your summation() method more compact and efficient as follows:

public static void summation(int n)
{
    int sum = n * (n + 1) / 2;
    System.out.println("Sum: " + sum);
}

However, if this is an assignment to practice for loops, then you shouldn't do this.

Yeah he chose a sample problem from the loops chapter so I went ahead and did loops rather than that formula.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.