0

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?

2
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by pattmorter
0

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.

Edited by mvmalderen

0

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);

    }

}
0

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.

Edited by mvmalderen

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.