2016-03-25 2 views
0

У меня проблема с моей программой.ошибка с объектом сканера java.util.NoSuchElementException

Ошибка: «java.util.NoSuchElementException: строки не найдено». Я ищу решение для переполнения стека и google, но я не нашел решения.

Мой исходный код:

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(); 

    } 

} 

Спасибо за помощь,

+0

вы можете распечатать трассировку стека, пожалуйста? – rsabir

+0

обеспечивают stacktrace – itpr

+1

Перед вызовом 'nextLine()', проверьте, есть ли строка с 'hasNextLine()'. – Berger

ответ

0

Вы не должны закрывать System.in. когда вы закрываете сканер, он внутренне закрывает System.in. Попробуйте комментируя

sc.close(); 

в Menu.Java

для более детальной проверки ответов на это Exception in thread "main" java.util.NoSuchElementException: No line found - Using scanner input

+0

Thans для ответа :), но если я понимаю, я никогда не закрываю объект сканера? –

+0

Внутри вашего класса меню вы выполняете 'sc.close();' который внутренне закрывает System.in. –

Смежные вопросы