0

I've been working on writing and calling some methods, one that shows the current date and one that prints an asterisk pyramid. I've gotten to the point that the code will compile, but I've come across another problem. When I attempt to run in JGrasp, I get the error: The following file is missing class or inner class files: (file name). Proceeding before correcting this is not recommended.. Not finding anything about this on the internet, I tried again but in Eclipse. The error message I got this time is: Selection does not contain a main type.

I would really appreciate it if someone could overview my code and tell me if they see the problem. An explanation would also be awesome, as I'm trying to learn and better myself as a programmer. Thanks!!

import java.util.Calendar;

public class prcMeth {

    public static void showCurrentDate() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DATE);
        int year = cal.get(Calendar.YEAR);
        System.out.println(year + "-" + month + "-" + day + " ");
    }

    public static boolean printPyramid(int n) {
        int number = n;
        if (number <= 0 || number > 10) {
            System.out.println("Your parameter is invalid; you input n = " + n);
            return false;
        }
        for (int row = 1; row <= number; row++) {
            for (int space = number; space > row; space--) {
                System.out.print(" ");
            }
            for (int star = 1; star <= row; star++) {
                System.out.print("*");
                System.out.println();
            }
        }
        return true;
    }

    public static void main(String[] args) {

        System.out.println("Hello, world!");

        // Calling first function
        showCurrentDate();

        // Calling second function
        System.out.println("n=0");
        printPyramid(0);

        System.out.println("n=1");
        printPyramid(1);

        System.out.println("n=5");
        printPyramid(5);

        System.out.println("n=10");
        printPyramid(10);

        System.out.println("n=11");
        printPyramid(11);
    }
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
user2130057
  • 406
  • 2
  • 5
  • 22
  • 1
    The message means that Eclipse cannot find a main method. Probably you are running the project without specifying a main class. You can avoid it by pressing F11 when editing the file with the main method. – Igor Rodriguez Aug 23 '13 at 06:00
  • 1
    works OK for me in Eclipse. Usually class named begin with an upperCase letter though. Also the methods do not need to be static. If they are not static you should instantiate an Object of your class. – Scary Wombat Aug 23 '13 at 06:02
  • possible duplicate of [Error: Selection does not contain a main type](http://stackoverflow.com/questions/16225177/error-selection-does-not-contain-a-main-type) – jww Sep 30 '14 at 05:48

3 Answers3

0

I think there is no any issue with this program because when i copy this program in notepad and try to run this than it run successfully. so No issue with this code.

The only one logical issue is

for (int star = 1; star <= row; star++) {

           System.out.print("*");

            System.out.println();

       }

should be

for (int row = 1; row <= number; row ++){

       for (int space = number; space > row; space--){

           System.out.print(" ");

        }

        for (int star = 1; star <= row; star++) {

           System.out.print("*");

       }
       System.out.println();

  }

to print pyramid in proper format. with the current code all star print in new line as your System.out.println() is inside the inner loop.

newuser
  • 8,338
  • 2
  • 25
  • 33
0

I have tested your code is working fine. In eclipse if you are having any problem to run the code , then it might be eclipse is not able to fine the main method. So do one thing right click on the code and select run as Run configuration and There you can provide the main class and all other entry related to JVM

Krushna
  • 5,059
  • 5
  • 32
  • 49
0

The problem you describe can easily happen in Eclipse when you have a pane other than the actual class focused. For example, even if you have your class open in the editor, and it's the only class open, if you have (for example) the Console pane focused, Eclipse will look for a main in the Console and, of course, fail to find one.

James Corcoran
  • 320
  • 1
  • 5