0

I started learning JAVA a couple of days ago, so my question might be too basic.

I have created a piece of code which is as follows:

import java.util.Scanner;

public class Que01 {

    public static void main(String[] args) {

        int principle=acceptInt("Principle");
        int roi=acceptInt("Rate Of Interest");
        int years=acceptInt("Years");
        float si=simpleInterest(principle,roi,years);

        System.out.println("Simple Interest for given details is : "+si);
    }

    static int acceptInt(String s1)
    {   System.out.println("Please Enter value for "+s1+" :");
        Scanner sc = new Scanner(System.in);
        int i= sc.nextInt();
        return i;
    }
    static float simpleInterest(int p,int r, int yr)
    {
        return p*yr*r/100;
    }

}

I want to know where should I write:

sc.close();

Also:

Any other suggestion to code for improvement are Welcome!

theSwapnilSaste
  • 180
  • 1
  • 13
  • 1
    The title suggests there is a loop, but I don't see any. – Scott Hunter Feb 14 '20 at 02:59
  • You are creating a new `Scanner` each time you call `acceptInt`. Don't create a new `Scanner` each time, reuse the same one. – Dioxin Feb 14 '20 at 03:00
  • You should never close `System.in`. See https://stackoverflow.com/q/14142853/1553851 – shmosel Feb 14 '20 at 03:01
  • @VinceEmigh, where should I create the scanner? If I create it in main function then I am unable to use it. Please could you write a better code version for me? – theSwapnilSaste Feb 14 '20 at 03:07
  • @theSwapnilSaste You could create the `Scanner` in `main` and pass it as an argument to `acceptInt`. Or you could create a static field variable. What you should *really* do is define a type, maybe `InputAcceptor`, which will own the `Scanner`. The `InputAcceptor` could then define the `acceptInt`. You'd instantiate an `InputAcceptor` in main & tell it to `acceptInt` (call the method). – Dioxin Feb 14 '20 at 03:24
  • @VinceEmigh, I got your first Approach of passing `Scanner` as an argument, but I am not getting the rest approaches, would you please elaborate with a code? I am a newbie. – theSwapnilSaste Feb 14 '20 at 03:29
  • You shouldn't close the `Scanner` at all. It's just a class, not a resource. The resource is `System.in` and closing the `Scanner` will also close the resource. It's suppose to be closed by the `JVM` when the program exists. – Scratte Feb 24 '20 at 05:49

4 Answers4

0
import java.util.Scanner;

public class Que01 {

    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        int principle = acceptInt("Principle");
        int roi = acceptInt("Rate Of Interest");
        int years = acceptInt("Years");
        float si = (p * yr * r/100);

        System.out.println("Simple Interest for given details is : " + si);
    }

    static int acceptInt(String s1) {   
        System.out.println("Please Enter value for " + s1 + " :");

        int i = sc.nextInt();

        sc.close();

        return i;
    }
}

This is what I would say but I'm not an expert. sc.close just saves anything you did with the scanner, eg. writing to a file and saving the file. - thus in this scenario I wouldn't even use it. I also wouldn't use a method to calc the si unless you plan to use it elsewhere as it's just taking up more space

Erfan Ahmed
  • 1,404
  • 4
  • 20
  • 30
T LJ
  • 11
  • 2
  • It throws errors:---Please Enter value for Principle : 1000 Please Enter value for Rate Of Interest : Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Assignments.Que01.acceptInt(Que01.java:20) at Assignments.Que01.main(Que01.java:9) – theSwapnilSaste Feb 14 '20 at 03:10
  • retry, had to format a few things EDIT: looks like you have a lot more uses of scanner - thus I wouldn't close it until you're 100% done with it, if you need to close it at all that is – T LJ Feb 14 '20 at 03:11
  • You are putting the scanner above the class. It should be inside the class. – Dioxin Feb 14 '20 at 03:14
0

import java.util.Scanner;

public class Que01 {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int principle = acceptInt("Principle", false, sc);
    int roi = acceptInt("Rate Of Interest", false, sc)
    int years = acceptInt("Years", true, sc);
    float si = simpleInterest(principle, roi, years);

    System.out.println("Simple Interest for given details is : " + si);
}


static int acceptInt(String s1, boolean closeScanner, Scanner sc) {
    System.out.println("Please Enter value for " + s1 + " :");

    int i = sc.nextInt();
    if (closeScanner)
        sc.close();
    return i;
}
static float simpleInterest(int p, int r, int yr) {
    return p * yr * r / 100;
}

}

i am not sure if this is a good practice of passing boolean as parameter to decide to close scanner

Nandini
  • 1
  • 1
0

You should not close the scanner do not worry about that it will terminated after calculating the simpleInterest. When you run the program and after entering required values it will calculate and return the result and quit. In you code 1 improvement is you should not create Scanner object again and again there should be only 1 object of Scanner throughout the life cycle.

Below is your updated code -

public class Que01 {
private static Scanner sc = null;
public static void main(String[] args) {
    sc = new Scanner(System.in);
    int principle=acceptInt("Principle");
    int roi=acceptInt("Rate Of Interest");
    int years=acceptInt("Years");
    float si=simpleInterest(principle,roi,years);

    System.out.println("Simple Interest for given details is : "+si);
}

static int acceptInt(String s1)
{   System.out.println("Please Enter value for "+s1+" :");

    int i= sc.nextInt();
    return i;
}
static float simpleInterest(int p,int r, int yr)
{
    return p*yr*r/100;
}

}

Create Scanner on start of the program and use it again and again. thats it

hope this will help you.

Dipankar Baghel
  • 1,596
  • 1
  • 9
  • 21
0

Thanks to Vince, I was able to create a good version of my code. and this is the answer I needed.


public class Que01 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int principle=acceptInt(sc,"Principle");
        int roi=acceptInt(sc,"Rate Of Interest");
        int years=acceptInt(sc,"Years");
        sc.close();
        float si=simpleInterest(principle,roi,years);

        System.out.println("Simple Interest for given details is : "+si);
    }

    static int acceptInt(Scanner sc,String s1)
    {   System.out.println("Please Enter value for "+s1+" :");

        int i= sc.nextInt();
        return i;
    }
    static float simpleInterest(int p,int r, int yr)
    {
        return p*yr*r/100;
    }

}```
theSwapnilSaste
  • 180
  • 1
  • 13