0
import java.util.*;

public class Main {
  static void factorFinder() {
    Scanner sc = new Scanner(System.in);
    boolean valid = false;
    int number = 0;
    
    
    while(! valid ){
      System.out.println("Enter the number you want to find the factor of(Numbers only)");
      try {
        number = sc.nextInt(); 
        valid = true;
      } catch (InputMismatchException e) {
            System.out.println("Not a number.");
            sc.next();
    }
      }
    sc.close();
    
    System.out.print("Factors of " + number + " are: ");
    for (int i = 1; i <= number; ++i) {

      // if number is divided by i
      // i is the factor
      if (number % i == 0) {
        System.out.print(i + " ");
      }
    }
  }
  public static void main(String[] args) {
    Scanner choiceScanner = new Scanner(System.in);
    System.out.println("Do you want to use the factor finder? (y/n)");
    String answer = choiceScanner.nextLine();
    choiceScanner.close();
    if(answer.equals("y")){
      factorFinder();
    }else{
      System.exit(0);
    }
    }
}

Here is the terminal output(Ran through replit)

 sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java')
 java -classpath .:target/dependency/* Main
Do you want to use the factor finder? (y/n)
y
Enter the number you want to find the factor of(Numbers only)
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 Main.factorFinder(Main.java:13)
    at Main.main(Main.java:38)
exit status 1
 

I am making a calculator that finds the factor of a given number. The calculator part is done and is working. However, the part where it asks to use the calculator isn't working. It gave a NoSuchElementException error. Can someone maybe help me on this?

  • It is alway better to show your terminal output on error to give an overview on what your compiler is catching. – Big Zed May 19 '22 at 16:22
  • Don't close the Scanner in the `main()` method. As a matter of fact, use only one scanner and only close it when you know your application will never need it again for as long as it is running. Once you close a Scanner object...you can't open another unless you restart your application. Declare your Scanner object as a class member so that all methods can use it. That way you only need the one Scanner object. – DevilsHnd - 退職した May 19 '22 at 16:32
  • you can find an helpful answer here : https://stackoverflow.com/questions/54393770/fixing-java-util-nosuchelementexception-for-scanner – Big Zed May 19 '22 at 16:47
  • Never close `System.in`, `System.out` or `System.err`: you won't be able to read from them anymore (hence the `NoSuchElementException` you're encountering). – MC Emperor May 19 '22 at 18:05

2 Answers2

1

Once you close a Scanner, you cannot read form another one.

So, you have two possible solution (as I can see):

1 (Recomended). Use only one Scanner. You can make it class member or pass it to the method as a parameter.

import java.util.*;

public class Main {
  static Scanner sc = new Scanner(System.in);
  static void factorFinder() {
    boolean valid = false;
    int number = 0;
    
    
    while(! valid ){
      System.out.println("Enter the number you want to find the factor of(Numbers only)");
      try {
        number = sc.nextInt(); 
        valid = true;
      } catch (InputMismatchException e) {
            System.out.println("Not a number.");
            sc.next();
    }
      }
    System.out.print("Factors of " + number + " are: ");
    for (int i = 1; i <= number; ++i) {

      // if number is divided by i
      // i is the factor
      if (number % i == 0) {
        System.out.print(i + " ");
      }
    }
  }
  public static void main(String[] args) {
    System.out.println("Do you want to use the factor finder? (y/n)");
    String answer = choiceScanner.nextLine();
    if(answer.equals("y")){
      factorFinder();
    }else{
      sc.close();
      System.exit(0);
    }
    sc.close();
    }
}
  1. Close the choiceScanner only after you call your factorFinder() method.
Programmer
  • 803
  • 1
  • 6
  • 13
0
public class Main2 {
    static Scanner sc = new Scanner(System.in);
    static void factorFinder() {
        boolean valid = false;
        int number = 0;


        while(! valid ){
            System.out.println("Enter the number you want to find the factor of(Numbers only)");
            try {
                number = sc.nextInt();
                valid = true;
            } catch (InputMismatchException e) {
                System.out.println("Not a number.");
                sc.next();
            }
        }
        System.out.print("Factors of " + number + " are: ");
        for (int i = 1; i <= number; ++i) {

            // if number is divided by i
            // i is the factor
            if (number % i == 0) {
                System.out.print(i + " ");
            }
        }
    }
    public static void main(String[] args) {
        Scanner choiceScanner = new Scanner(System.in);
        System.out.println("Do you want to use the factor finder? (y/n)");
        String answer = choiceScanner.nextLine();
        if(answer.equals("y")){
            factorFinder();
        }else{
            sc.close();
            System.exit(0);
        }
        sc.close();
    }
}
katyRosale
  • 64
  • 3