0

I am working on some basic java skills are I am getting a NoSuchElementExemption when I am debugging. My goal is to ask two questions in two different methods and combine them in the main function. Could someone explain the rule that I am breaking?

The code is as follows:

import java.util.Scanner;

public class TwoInputs{

    public static double test()
    {
        Scanner reader2 = new Scanner(System.in);
        System.out.println("Enter a number ");

        double n2 = reader2.nextDouble();
        if (n2 %1 != 0) {
            System.out.println("Number is invalid");    
        } else {
            reader2.close(); 
            System.out.println("You put the number " + n2); 
        }
        return n2;
    }

    public static double test2()
    {
        Scanner reader1 = new Scanner(System.in);
        System.out.println("Enter a number ");

        double n1 = reader1.nextInt();
        if (n1 %1 != 0) {
            System.out.println("Number is invalid");
        } else {
            reader1.close(); 
            System.out.println("You put the number " + n1); 
        }
        return n1;
    }

    public static void main(String[] args) {
        double sum = test();
        double sum2 = test2();
        System.out.println("You put the number " + sum+ "and"+ sum2);   
    }
}
moondaisy
  • 3,655
  • 4
  • 30
  • 59
Kerry
  • 127
  • 10
  • Possible duplicate of [Close a Scanner linked to System.in](https://stackoverflow.com/questions/14142853/close-a-scanner-linked-to-system-in) – Coder-Man Jul 26 '18 at 17:41
  • You close System.in and try to reuse it, the link I provided above explains a workaround for this. – Coder-Man Jul 26 '18 at 17:41
  • Possible duplicate of [NoSuchElementException with Java.Util.Scanner](https://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner) – Sedrick Jul 26 '18 at 17:53
  • @Sedrick that's not it. The problem is that the OP is reusing a closed System.in. – Coder-Man Jul 26 '18 at 17:55

1 Answers1

0

I think by closing the reader, the inputstream will be closed, too. I.e. you can not open an other Scanner with System.in. Try it this way:

import java.util.Scanner;

    public class Main {
public static double test(Scanner scanner)
{
    System.out.println("Enter a number ");

    double n2 = scanner.nextDouble();
    if (n2 %1 != 0) {
        System.out.println("Number is invalid");

    } else {
        System.out.println("You put the number " + n2);
    }
    return n2;
}

public static double test2(Scanner scanner)
{
    System.out.println("Enter a number ");
    double n1 = scanner.nextInt();
    if (n1 %1 != 0) {
        System.out.println("Number is invalid");

    } else {
        System.out.println("You put the number " + n1);
    }
    return n1;
}

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    double sum = test(scanner);
    double sum2 = test2(scanner);
    System.out.println("You put the number " + sum+ "and"+ sum2);

}
}
  • Yes, this was very helpful. I would have never tried not closing the reader. Do you know if there has to be a ".close" statement in the code? – Kerry Jul 26 '18 at 17:57