'java.util.scanner used multiple times

I already have a scanner established in my program. It looks like this:

Scanner in = new Scanner(System.in);

Later on in the program I need to use the same scanner to get more information from the user. Is there a way to use this same scanner without creating another one?

This is the last question that I have and I need to use a scanner to grab the input whether it is a "Y" or an "N"

System.out.print("Would you like to delete this account? (Y/N) ");
        String input = in.next();
        if (input.equalsIgnoreCase("Y")) { done = true; }


Solution 1:[1]

I wouldn't create a new Scanner, just reuse the Scanner that you created. For instance you can use the same Scanner to give values to different Strings based on user input.

Ex:

Scanner in = new Scanner(System.in);
String first = in.next();
String second = in.next();

Does this help?

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 Hanlet Escaño