I have a greetingLoop is called on by a switch case in main(), and if it returns null, user input is asked for once but doesnt do anything. I am trying to avoid the greetingLoop() from returning a null, but as soon as I put the return variable userInput in an if statement, it isnt recognized by eclipse "this method must return a type of int."
Any hints would be greatly appreciated, thanks.
public static int greetingLoop(){
int userInput=0;
try{
System.out.println("Please enter the desired operation:");
System.out.println("1.Insert, 2.Delete, 3.Edit, 4.Show, 5.List, 6.Volume, 7.Exit\n");
userInput=Integer.parseInt(JOptionPane.showInputDialog("Please enter the desired
operation:"));
}
catch (NumberFormatException ne){
System.out.println("your input " + userInput + " is not valid. Please try again...\n");
}
if(userInput >= 1 && <= 7){
return userInput;
}
}
This is because you are not returning anything in the case where the if condition predicate was false
add return userInput;
after the }
of the if
statement
at any rate, you cannot ever return null
in your current method because your return type is primitive int
and will always return an int value.
so, in this case if you add return userInput;
as the last line in your method, it will return the initialized value 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.