Question:
DrJava BOOLEAN Error: This class does not have a static void main method accepting String[].?
Follow @deni_in_al
2013-10-04 17:48:57 UTC
import java.util.*; // for Scanner

public class negativeSum {
boolean negativeSum;
public static boolean main(String line) {
Scanner lineScan = new Scanner(line);

int sum = 0;
int count = 0;
while (lineScan.hasNextInt()) {
int next = lineScan.nextInt();
sum += next;
count++;
if (sum < 0) {
System.out.println(sum + " after " + count + " steps");
return true;
}
}
System.out.println("no negative sum");
return false; // not found
}
}
Three answers:
Leo D
2013-10-04 20:38:53 UTC
The "main function" in Java main(String[]) has to be:

- public

- static

- void

- called main

- and accepting and only an array of strings as its arguments.



If you deviate from that in any way, it won't work. An example is: public static void main(String[] line)



That means you have to change a few things in your program.



Scanner only accepts strings, not arrays of strings. So build a string from that array, as so:

    StringBuilder joinedLine = new StringBuilder();

    for (String word : line) {

        joinedLine.append(word).append(" ");

    }

    Scanner lineScan = new Scanner(joinedLine);



Of course it is possible that you could just do this instead:

    

    try {

        for (String word : line) {

            Scanner wordScan = new Scanner(word);

            while (lineScan.hasNextInt()) {

                int next = lineScan.nextInt();

                sum += next;

                count++;

                // ......

            }

        }

    } catch (NumberFormatException e) {}



The next thing you have to change is that you can't return a value from main(String[]). You could throw an Error, a Runtime Exception, a declared Exception or even call System.exit(int) which accepts an integer status. A nonzero status means there was an abnormal termination. (So System.exit(0); is effectively the same as return;.)
Chris
2013-10-05 01:12:30 UTC
The main method must have an array of Strings as parameter and a return type of void:



public static void main(String[] line) {



If there's no method with that structure, you can't run the class.



Note that if you run the program using something like

java negativeSum.class 1 2 3 4 5

line will have five elements, with line[0] being "1", line[1] being "2", etc. as Strings.



To get the ints, just do this:



for (String s : line) {

int next = Integer.parseInt(s);

...

}



Finally, don't return anything from the main method, not even true or false. It has to be void.
AJ
2013-10-05 01:11:46 UTC
In Java, when the program is compiled and executed, the main method is executed first. In your code here you did not include the main method which lead to the error since the program has nothing to execute. If there is not main method, then the code is not executable. You must include the main method; public static void main(Strings[] args){}



Within the curly brackets, you put in the code you wish to execute or you can leave the code as it is not, create the main method by including the line above and then creating an instance of the boolean method you have there. Then you can execute whatever is within that method by referencing to it using the instance you created.



Happy Programming :D


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...