I'm trying to learn the basics of Java when I have nothing to do at work, and I wanted to play around with input. This is what I have now:
import java.util.Scanner;
public class Input {
private static Scanner input;
public static void main(String [] args){
String name = (askName());
double age = (askAge());
System.out.println("Your name is " + name + " and your age is " + age);
}
static String askName(){
System.out.print("What is your name?");
input = new Scanner(System.in);
//listens for strings
String name = input.next();
return name;
}
static double askAge(){
System.out.print("What is your age?");
input = new Scanner(System.in);
//listens for doubles
double age = input.nextDouble();
if (input.hasNextDouble()){
return age;
} else {
System.out.println("Please insert a number:");
askAge();}
}
}
And this is what I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type double
at Input.askAge(Input.java:16)
at Input.main(Input.java:6)
What do I do to force the user to enter an integer (that is, how do I make the method repeat itself until it gets an int that it can return?)