-2

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?)

4 Answers4

2

Return the value of the recursive call:

System.out.println("Please insert a number:");
return askAge();

However: it would be better to use a loop rather than recursion here, since you could (eventually) get a StackOverflowError if you keep on entering an invalid value.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

If condition is used for decision making and not for looping. Use a while loop , Break the loop when u get the correct value. I am wiring a sudo code.

while(true){
if (input.hasNextDouble()){
        //Assign value to a variable ;
        //Break loop
    } else {
        //Re ask question 
        System.out.println("Please insert a number:");
       }

}

Example link for your help.

The exception you are getting is because you are not returning any value from askAge() in else part.

Community
  • 1
  • 1
Nimble Fungus
  • 513
  • 3
  • 22
  • Ok, so I managed to get the method to ask me for a double until I enter one, but if I don't do it the first time I'm asked, it doesn't return to the main method. What am I doing wrong now? Please don't shoot, I'm just a linguist surrounded by IT guys ;) – kukunamuniu Dec 21 '15 at 14:16
0

I would rather do it this way:

public static void main(String [] args){
    Scanner sc = new Scanner(System.in);
    String name = getString(sc, "What is your name? ");
    int age = getInt(sc, "What is your age? ");
    System.out.println("Your name is " + name + " and your age is " + age);
}


/* You increase the performance when using an already existing single      
scanner multiple times for different reasons (to get a name, first name, 
second name, age, etc.), instead of making a new Scanner each time */

public static String getString(Scanner sc, String message) {
    System.out.print(message);
    return sc.nextLine();
}

public static int getInt(Scanner sc, String message) {
    System.out.print(message);
    if (sc.hasNextInt()) {
        return sc.nextInt();
    } else {
        sc.next(); // required to skip the current input
        return getInt(sc, "Please insert a number: ");
    }
}
LowLevel
  • 1,085
  • 1
  • 13
  • 34
-1

Please change your mathod like,

   static double askAge(){
    System.out.print("What is your age?");
    input = new Scanner(System.in);
    //listens for doubles

    if (input.hasNextDouble()){
        return input.nextDouble();
    } else {
        System.out.println("Please insert a number:");
       }
    return  askAge();
}}
Bharat DEVre
  • 539
  • 3
  • 13