【问题标题】:error with scanner object java.util.NoSuchElementException [duplicate]扫描仪对象 java.util.NoSuchElementException 出错 [重复]
【发布时间】:2016-03-25 13:04:09
【问题描述】:

我的程序有问题。

错误是“java.util.NoSuchElementException:找不到行”。我正在搜索堆栈溢出和谷歌的解决方案,但我没有找到解决方案。

我的源代码是:

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

    }

}

感谢您的帮助,

【问题讨论】:

  • 你能打印堆栈跟踪吗?
  • 提供堆栈跟踪
  • 在调用nextLine()之前,请检查是否有一行hasNextLine()
  • Java != Javascript(关于您的代码 sn-ps)。
  • 你试过这个解决方案了吗? stackoverflow.com/questions/13102045/…

标签: java


【解决方案1】:

您不应关闭 System.in。当您关闭 Scanner 时,它会在内部关闭 System.in。尝试评论

sc.close();

在 Menu.Java 中

更多详细答案请查看Exception in thread "main" java.util.NoSuchElementException: No line found - Using scanner input

【讨论】:

  • 感谢答案 :) 但如果我理解,我永远不会关闭扫描仪对象?
  • 在你的菜单类中你正在做sc.close();,它在内部关闭了 System.in。
猜你喜欢
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2013-02-08
相关资源
最近更新 更多