Does anyone know why this method will not let me call it. I'm trying to make sure I can call a method with no parameters before I start writing my code. Is there some package or something I need or can someone explain what's going on. Thanks in advance. I'm getting an java: illegal start of expression and red line under () next to validate.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
CodePudding user response:
The method validate
may not be defined inside the main
method.
Instead, do it like this:
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
CodePudding user response:
You have your curly brace order mixed up; and in doing so, have declared a new method within the main method.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
You should declare your methods at the class level.
Change your code to this:
import java.util.Scanner; import java.lang.Math;
/// Start Program
public class javaMethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
}