0

I have some code in which multiple methods use the keyboard, and are called consecutively in the main method. The exercise I am doing specifically asks for the 4 separate methods so I cannot put them altogether. Originally, I used keyboard.close() at the end of each method, but this would cause a NoSuchElementException when the second method is run, regardless of the order they're called. By removing the keyboard.close(), the code now works, however I now have warnings for a resource leak as the keyboard isn't closed. Can anyone point me to a way to close the inputs without getting an error? Any help would be greatly appreciated.

import java.util.*;

public class CurrencyConverter {
    static double money;
    static double xr;

    //Running 
    public static void main(String[] args)
    {

        requestAmount();
        requestRate();
        displayResult();

    }

    //Retrieving amount of money to be exchanged based on user input
    public static void requestAmount()
    {
        System.out.println("Please enter quanitity of money to be exchanged:");
        Scanner keyboard = new Scanner(System.in);
        money = keyboard.nextDouble();
    }

    //Retrieving exchange rate from user input
    public static void requestRate()
    {
        System.out.println("Please enter exchange rate:");
        Scanner keyboard = new Scanner(System.in);
        xr = keyboard.nextDouble();
    }

    //Converting currency
    public static double conversionCalculator(double moneyIn, double xrIn)
    {
        return moneyIn*xrIn;
    }

    //Display amount of converted currency
    public static void displayResult()
    {
        System.out.println("Amount of currency post-conversion = " + conversionCalculator(money, xr));
    }


}
    
/*
Design and implement a program that converts a sum of money to a different
currency. The amount of money to be converted, and the exchange rate, are
entered by the user. The program should have separate methods for:
• obtaining the sum of money from the user;
• obtaining the exchange rate from the user;
• making the conversion;
• displaying the result.
*/
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
  • I'm confused. Did you use ``keyboard.close()`` on _all four_ functions? Also note that Java has garbage collection, though I may be misunderstanding the context of "resource leak". – Leaderboard Jul 19 '22 at 15:02
  • 1
    Don't use `double` for currency [Why not use Double or Float to represent currency?](https://stackoverflow.com/q/3730019/2670892) – greg-449 Jul 19 '22 at 15:02
  • 2
    You should not close a `Scanner` that is wrapping `System.in` since you did not open `System.in`. If you are getting a warning, the warning is simply incorrect. However, there is no need to create it four times. You can create one `Scanner` in your `main` method and pass it as a parameter to all the methods that need to use it. – David Conrad Jul 19 '22 at 15:06

1 Answers1

1

You don't need to close Scanner that is wrapping System.in as David Conrad pointed in the comments. Normally you should have one Scanner associated with System.in. On your place I will created it in main method and pass it as parameter in the methods.

If you really do not want to be show this warning you can add @SuppressWarnings("resource") on the top of the scanner.

 //Running 
public static void main(String[] args) 
{
    @SuppressWarnings("resource")
    Scanner keyboard = new Scanner(System.in);
    requestAmount(keyboard);
    requestRate(keyboard);
    displayResult();
    
}
Level_Up
  • 789
  • 5
  • 19