'method validateAge in class UserValidator cannot be applied to given types; validator.validateAge();
I need some explanation about what have I done wrong and how to do it better. Sometimes it's difficult for me to understand everything.
public class UserValidator {
public void validateName(String name) {
if (name == null)
System.out.println("Set your name");
else {
System.out.println("User name: " + name);
}
}
public void validateAge(double age, double height) {
if (age > 30 && height > 160) {
System.out.println("User is older than 30 and higher ten 160cm");
} else {
System.out.println("User is younger than 30 or lower than 160");
}
}
public class Application {
public static void main(String[] args) throws java.lang.Exception {
System.out.println("Starting");
String name = "Adam";
double age = 40.5;
double height = 178;
UserValidator validator = new UserValidator();
validator.validateName(name);
validator.validateAge(age, height);
System.out.println("Finishing");
}
}
}
:19:5 java: illegal start of expression :45 java: class, interface, enum, or record expected**
^
The examples give me little understanding, but with my own work it is much harder if someone could explain it to me on the example of my mistakes for a better understanding of the issue.
Solution 1:[1]
I see two errors in your code. First, in your validateName you are performing the wrong check, if name IS NULL then you should set the name otherwise you should print it, your code does the opposite. The correct method is the following:
public void validateName(String name) {
if (name == null)
System.out.println("Set your name");
} else {
System.out.println("User name: " + name);
}
The second error is that you are not passing any variable to your methods. validateName requires a string as input parameter, but you are not passing any value, therefore the error. You have to pass the variables in this way:
validator.validateName(name);
validator.validateAge(age, height);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | molfo |