0

I'm trying to call the start () method in this own method using Scanner. Once the user enters a number a sequence of actions are performed and then I would like the start () method to be called again. Except I get this error :

enter image description here

My code :

    public void start() {
    System.out.println("1. Créer un nouveau réseau");
    System.out.println("2. Créer un nouvel ordi portable");
    System.out.println("3. Créer un nouvel ordi fix");
    System.out.println("4. Créer un Switch \n");
    System.out.print("Choisir un chiffre : ");
    Scanner scan = new Scanner(System.in);
    if(scan.hasNextInt()) {
        int v = scan.nextInt();
        switch(v) {
            case 1:
                addNetwork();
                scan.next();
                start();
                break;
        }
    } else {
        System.out.println("Vous devez rentrer un chiffre !");
        scan.next();
        start();
    }
}

EDIT :

Here is the full output of the program :

enter image description here

EDIT :

My addNetwork method :

    private void addNetwork() {
    System.out.print("Quel est l'IP du réseau ? ");
    Scanner scan = new Scanner(System.in);
    String ip = scan.nextLine();
    System.out.print("Quel est le masque du réseau ? (par défaut /24) ");
    String mask = scan.nextLine();
    scan.close();
    web.addNetwork(ip, mask);
}

2 Answers2

1

Your addNetwork method is troublesome.

In addNetwork method you are closing the scanner, it not only closes your scanner but closes your System.in input stream as well.

References :

java.util.NoSuchElementException - Scanner reading user input

I have updated the code and i would suggest to initialize scanner object once and pass as an argument to your start method and close the scanner once you are done.

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.stream.Stream;

public class Main {

    private void addNetwork(Scanner scan) {
        System.out.print("Quel est l'IP du réseau ? ");
        String ip = scan.nextLine();
        System.out.print("Quel est le masque du réseau ? (par défaut /24) ");
        String mask = scan.nextLine();
        web.addNetwork(ip, mask);
    }

    public void start(Scanner scan) {
        System.out.println("1. Créer un nouveau réseau");
        System.out.println("2. Créer un nouvel ordi portable");
        System.out.println("3. Créer un nouvel ordi fix");
        System.out.println("4. Créer un Switch \n");
        System.out.print("Choisir un chiffre : ");
        if (scan.hasNextInt()) {
            int v = scan.nextInt();
            switch (v) {
                case 1:
                    addNetwork(scan);
                    scan.next();
                    start(scan);
                    break;
            }
        } else {
            System.out.println("Vous devez rentrer un chiffre !");
            scan.next();
            start(scan);
        }
    }

    public static void main(String args[]) {
        Main main = new Main();
        Scanner scan = new Scanner(System.in);
        main.start(scan);
        scan.close();
    }
}

Output :

1. Créer un nouveau réseau
2. Créer un nouvel ordi portable
3. Créer un nouvel ordi fix
4. Créer un Switch 

Choisir un chiffre : 1
Quel est l'IP du réseau ? Quel est le masque du réseau ? (par défaut /24) 192.168.0.21
TestProgram
1. Créer un nouveau réseau
2. Créer un nouvel ordi portable
3. Créer un nouvel ordi fix
4. Créer un Switch 

Choisir un chiffre : 
SRJ
  • 2,092
  • 3
  • 17
  • 36
  • The program works fine in itself but there are 2 problems, the first is that the two questions in the addNetwork () method are asked at the same time and that the start () method does not restart once the operations are finished –  Feb 21 '21 at 08:35
  • 1
    It's all good I just solved the problem with your help thank you very much! –  Feb 21 '21 at 08:38
0

The problem is in your addNetwork method, where you call scan.close(). This in its turn calls System.in.close() so no characters can be read from standard input any more.

In addNetwork your should pass the scanner you already have:

private void addNetwork(Scanner scan) {
    System.out.print("Quel est l'IP du réseau ? ");
    String ip = scan.nextLine();
    System.out.print("Quel est le masque du réseau ? (par défaut /24) ");
    String mask = scan.nextLine();
    web.addNetwork(ip, mask);
}
Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • I removed the scan.close () in the addNetwork method, the code works fine but it does not restart once done –  Feb 21 '21 at 08:27
  • 1
    In your `start()` method you call `scan.next()` after `addNetwork()`, so the scanner needs to consume an additional work before calling `start()`. – Piotr P. Karwasz Feb 21 '21 at 08:33