'Allow user input for the number of threads for Fibonacci Sequence and Factorial Calculation using multithreading

I want to use scanner for user input for the number of threads to run. So far, this java code is able to run both the number of the Fibonacci sequences and factorial calculation based on user input. How do I do allow user input for the number of threads?:

import java.io.IOException; import java.util.Scanner;

class Fibonacci extends Thread {

@Override
public void run() {
    
        
    int n1=0, n2=0, n3=1;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the amount of Fibonacci: ");
    int number = sc.nextInt();
    for(int i=1; i<=number; i++) {
        n1 = n2;                                
        n2 = n3;
        n3 = n1 + n2;
        System.out.println(n1 + "  ");
    }
        

        
        }
    }

class Factorial extends Thread {

@Override
public void run() {
    try {
        
            long fact = 1;
            int i = 1, n;
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a number for the calculation of Factorial:    ");
            n = input.nextInt();
            input.close();
            
            while(i<=n)
            {
                fact = fact * i;
                i++;
            }
            System.out.println("Factorial of "+n+" is: "+fact);
            
                
        
            
    } catch (NumberFormatException ex) {

    }
}

}

public class Assignment02 {

public static void main(String args[]) throws IOException {
    Scanner input = new Scanner(System.in);
    System.out.println("1.Fibonacci");
    System.out.println("2.Factorial");
    System.out.println("Choose your Option:");
    String line = input.nextLine();
    int option = Integer.parseInt(line);
    
    switch (option) {
        case 1:
            System.out.println("Fibonacci Sequence SELECTED");
            try {

                Fibonacci fib = new Fibonacci();
                fib.start();
                fib.join();
                

            } catch (Exception e) {
            }

            break;

        case 2:
            System.out.println("Factorial SELECTED");
            try {
                
                Factorial fact = new Factorial();
                fact.start();
                fact.join();
                
            } catch (Exception e) {
            }
            break;

        default:
            System.out.println("Choose a valid option");
            break;
    }
    

} }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source