public void AmPm(int time) {
if (time >= 12 && time < 12)
System.out.println("AM");
else if (time >= 12 && time < 24)
System.out.println("PM");
else
System.out.println("invalid input");}
How can I call this method in the main method
public void AmPm(int time) {
if (time >= 12 && time < 12)
System.out.println("AM");
else if (time >= 12 && time < 24)
System.out.println("PM");
else
System.out.println("invalid input");}
How can I call this method in the main method
Main method is static, and from static methods you can call only static ones. What you can do is:
class A {
public void amPm(int time) {
if (time >= 0 && time < 12) //you have a typo there
System.out.println("AM");
else if (time >= 12 && time <24)
System.out.println("PM");
else
System.out.println("invalid input");
}
//or as static method:
public static void amPmInStaticWay(int time) { //... }
public static void main(String[] args) {
//...
A a = new A();
a.amPm(time);
//or
amPmInStaticWay(time);
//or if you want to use static method from different class
A.amPmInStaticWay(time);
}
}
You need to create an instance of object of class A
because this method isn't static. Then you can call that method on that reference:
public static void main(String[] args) {
A a = new A();
a.amPm(time); /* instead of typing "time" you need to pass int value that you want to be passed to the method as an argument */
}
your method not static so you should create object from the class this method belongs to. The idea is : The static methods is at class level, so you can't call non static methods from static methods.