0

I have an issue with my program.

The error is "java.util.NoSuchElementException: No line found". I'm search a solution on stack overflow and google but i haven't find a solution.

My source code is :

FormInscreption.java

package ihm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class FormInscription {
    private String nom;
    private String prenom;
    private Date date_de_naissance;
    private String numero_etudiant;
    private String adresse;
    private String courriel ;
    private int numero_passport;
    private int numero_permis;
    private String role;
    private Scanner test2 = new Scanner(System.in);

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public Date getDate_de_naissance() {
        return date_de_naissance;
    }

    public void setDate_de_naissance(Date date_de_naissance) {
        this.date_de_naissance = date_de_naissance;
    }

    public String getNumero_etudiant() {
        return numero_etudiant;
    }

    public void setNumero_etudiant(String numero_etudiant) {
        this.numero_etudiant = numero_etudiant;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getCourriel() {
        return courriel;
    }

    public void setCourriel(String courriel) {
        this.courriel = courriel;
    }

    public int getNumero_passport() {
        return numero_passport;
    }

    public void setNumero_passport(int numero_passport) {
        this.numero_passport = numero_passport;
    }

    public int isNumero_Permis() {
        return numero_permis;
    }

    public void setNumero_Permis(int numero_permis) {
        this.numero_permis = numero_permis;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public void readDate() throws Exception{
        String dateFormat = "dd/MM/yyyy";
        setDate_de_naissance(new SimpleDateFormat(dateFormat).parse(test2.nextLine()));
        test2.close();
    }

    public FormInscription() {

        try {
            System.out.println("Entrer le nom :");
            this.nom = test2.nextLine();

            test2.close();
        }
        catch(Exception e){
                System.out.println("erreur : "+e);
        }
    }


}

Menu.java

package ihm;

import java.util.Scanner;

public class Menu {

    private int choix;
    private Scanner sc = new Scanner(System.in);
    public Menu(){
        System.out.println("==== MENU ====");
        System.out.println("1. Inscription");
        System.out.println("2. Consultation");
        System.out.println("3.Exit");
        System.out.println("==============");
        System.out.println("Entrez votre choix :");
        try{
            choix = sc.nextInt();
            sc.close();
            System.in.close();
        }
        catch(Exception e){
            System.out.println("Erreur :"+e);
        }


        switch(choix){
            case 1 :
                app.System.inscription();
                break;
            case 2 :
                System.out.println("Choix encore indisponible...");
                break;
            case 3 :
                System.exit(0);
                break;      
        }



    }
}

System.java

package app;
//import java.io.*;
import ihm.*;
public class System {

    // Fonction inscription
    public  static void inscription(){
        FormInscription test = new FormInscription();

    }

    public static void main(String[] args) 
    {
        java.lang.System.out.println("Bienvenue sur le gestionnaire du 4L Trophy");

        // On affiche le menu
        Menu menu = new Menu();

    }

}

Thank you for help,

Mad Physicist
  • 76,709
  • 19
  • 122
  • 186

1 Answers1

0

You should not close the System.in. when you close the Scanner, it internally closes the System.in. Try commenting

sc.close();

in Menu.Java

for more detailed answer check this out Exception in thread "main" java.util.NoSuchElementException: No line found - Using scanner input

Community
  • 1
  • 1
Deendayal Garg
  • 4,500
  • 1
  • 16
  • 33