1

I know that the question has been asked but I tried to apply what I saw here and got an error.

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner get_input = new Scanner(System.in);
        System.out.println("Enter your name ");
        String name = get_input.nextLine();


        boolean is_int = false;
        int year_of_birth = 0;
        System.out.println("Enter your year of birth");
        while (!get_input.hasNextInt()) {
        // If the input isn't an int, the loop is supposed to run
        // until an int is input.

            get_input.hasNextInt();
            year_of_birth = get_input.nextInt();
        }
        //year_of_birth = get_input.nextInt();

        System.out.println("Enter the current year");
        int current_year=get_input.nextInt();


        int age = current_year-year_of_birth;


        System.out.println("Your name is " + name + " and you are " + age + " year old.");

        get_input.close();

    }
}

Without the loop, everything works fine. What is wrong in my code? To be clear, I'm trying to ask for an input until the input can be validated as an integer.

Thanks a lot in advance.

5
  • 1
    that wgile loop makes no sense Commented Apr 12, 2020 at 20:02
  • As Antoniossss mentioned, you are not going to get anywhere with that loop. What you are doing is asking the scanner to parse a string to an int only if it can't be parsed. Try removing that exclamation mark or clarify what you are trying to do. Commented Apr 12, 2020 at 20:10
  • In the loop, I would like to check if the input is an integer and then assign it to the int year_of_birth. I get: "Exception in thread "main" java.util.InputMismatchException". I tried to use a boolean as an intermediate value and then assign the result of hasNextInt to the boolean but I wasn't able to do it. Commented Apr 12, 2020 at 20:11
  • and you are doing while(input is not an integer) => assign integer. Commented Apr 12, 2020 at 20:18
  • I'm trying to do "while(input is not an integer) try to ask for a new input and test whether or not this is an integer". I don't know if it's clear. Commented Apr 12, 2020 at 20:20

2 Answers 2

2

If you would like to skip invalid non-int values, your loop should look like this:

    while (!get_input.hasNextInt()) {
        // skip invalid input
        get_input.next();
    }
    // here scanner contains good int value  
    year_of_birth = get_input.nextInt();
Sign up to request clarification or add additional context in comments.

Comments

0

This works for me if i understood you correctly. You need to keep checking what value has the scanner, so you need to keep advancind through the scanner while the value is not an integer:

        Scanner get_input = new Scanner(System.in);
        System.out.println("Enter your name ");
        String name = get_input.nextLine();

        int year_of_birth = 0;

        System.out.println("Enter your year of birth");
        while (!get_input.hasNextInt()) { //check if it is not integer
            System.out.println("Enter your year of birth"); // ask again
            get_input.next(); //advance through the buffer
        }
        year_of_birth = get_input.nextInt(); //here you get an integer value
        int current_year=get_input.nextInt();
        int age = current_year-year_of_birth;
        System.out.println("Your name is " + name + " and you are " + age + " year old.");

        get_input.close();

enter image description here

7 Comments

Thanks. Could you explain me why my code wasn't working ?
Inside the while loop, you were assigning the value, if what you want to do is keep asking the user until you get an integer value, inside the while loop you need to skip all the values that you don't want. So if the function hasNextInt returns false we don't want that value so we skip it with next(). With this logic once you finish the loop it has to be because the value it is an integer.
Thanks. Why did I get the error "main" java.util.InputMismatchException"? And in your loop what's assigning the value to year_of_birth?
Whops, i forgot a line at the end to assign the value year_of_birth. I edited the post.
Thanks. Isn't it possible to print two messages in the loop? If I try to do so, the loop only runs once.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.