Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;没有这样的元素“;第二次运行方法时出错_Java_Java.util.scanner_Filewriter_Printwriter - Fatal编程技术网

Java &引用;没有这样的元素“;第二次运行方法时出错

Java &引用;没有这样的元素“;第二次运行方法时出错,java,java.util.scanner,filewriter,printwriter,Java,Java.util.scanner,Filewriter,Printwriter,抱歉,如果这是一个愚蠢的问题,我是一个编程高手,下面的代码有点问题 public static void main(String[] args) throws IOException { addName(); addName(); //Line "10" in error } public static void addName() throws IOException { //Initializing Variables and Scanner Sc

抱歉,如果这是一个愚蠢的问题,我是一个编程高手,下面的代码有点问题

public static void main(String[] args) throws IOException {
    addName();
    addName();      //Line "10" in error
}

public static void addName() throws IOException {
    //Initializing Variables and Scanner
    Scanner keyboard = new Scanner(System.in);
    FileWriter fwriter = new FileWriter("Data.txt", true);
    PrintWriter outputFile = new PrintWriter(fwriter);
    int numFriends;
    String firstName;
    String lastName;
    String email;

    //Get number of entries to add
    System.out.print("How many people do you wish to add? ");
    numFriends = Integer.parseInt(keyboard.nextLine());        //Line "25" in error

    //Prompt for info and add it to the file
    for(int i = 1; i <= numFriends; i++){
        System.out.printf("Enter the first name of person %d: ",i);
        firstName = keyboard.nextLine();
        System.out.printf("Enter the last name of person %d: ",i);
        lastName = keyboard.nextLine();
        System.out.printf("Enter the email of person %d: ",i);
        email = keyboard.nextLine();

        outputFile.println(firstName + " " + lastName + " " + email);
    }

    //Wrap things up and give a confirmation
    outputFile.close();
    keyboard.close();
    System.out.println("Data written to the file.");;
}

我原本以为这是出自一个没有在某处消费过的新词角色,但我似乎无法理解。为什么我会犯这个错误?我怎样才能修好它?谢谢大家!

您不应该在方法结束时关闭
扫描仪
,理想情况下,您应该在整个执行过程中重复使用相同的
扫描仪

通过关闭
扫描仪
,您也将关闭其输入源,即本例中的
系统。因此,用户不能再输入任何内容

以下是更正后的代码段:

public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    addName(keyboard);
    addName(keyboard);
}

public static void addName(Scanner keyboard) throws IOException {
    FileWriter fwriter = new FileWriter("Data.txt", true);
    PrintWriter outputFile = new PrintWriter(fwriter);
    int numFriends;
    String firstName;
    String lastName;
    String email;

    //Get number of entries to add
    System.out.print("How many people do you wish to add? ");
    numFriends = Integer.parseInt(keyboard.nextLine());        //Line "25" in error

    //Prompt for info and add it to the file
    for(int i = 1; i <= numFriends; i++){
        System.out.printf("Enter the first name of person %d: ",i);
        firstName = keyboard.nextLine();
        System.out.printf("Enter the last name of person %d: ",i);
        lastName = keyboard.nextLine();
        System.out.printf("Enter the email of person %d: ",i);
        email = keyboard.nextLine();

        outputFile.println(firstName + " " + lastName + " " + email);
    }

    //Wrap things up and give a confirmation
    outputFile.close();
    System.out.println("Data written to the file.");
}
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪键盘=新扫描仪(System.in);
地址名(键盘);
地址名(键盘);
}
公共静态void addName(扫描仪键盘)引发IOException{
FileWriter fwriter=新的FileWriter(“Data.txt”,true);
PrintWriter outputFile=新的PrintWriter(fwriter);
国际女朋友;
字符串名;
字符串lastName;
字符串电子邮件;
//获取要添加的条目数
System.out.print(“您希望添加多少人?”);
numFriends=Integer.parseInt(keyboard.nextLine());//行“25”出错
//提示输入信息并将其添加到文件中

例如(int i=1;我哇,我现在觉得自己很笨。非常感谢!关闭阅读器也会关闭输入源并不一定是直观的,但随着您经验的增加,您会意识到这通常是Java中的默认行为(我实际上不知道标准库中的任何反例)。如果这个答案有帮助,请随意投票/接受。原则是,你负责关闭你打开的任何东西。因此,你打开了文件
Data.txt
;你负责关闭它。你没有打开
系统。在
,你只在它周围包装了一个
扫描仪,但它已经由Java为你打开了(或者,实际上是操作系统)所以你不应该关闭它。
public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    addName(keyboard);
    addName(keyboard);
}

public static void addName(Scanner keyboard) throws IOException {
    FileWriter fwriter = new FileWriter("Data.txt", true);
    PrintWriter outputFile = new PrintWriter(fwriter);
    int numFriends;
    String firstName;
    String lastName;
    String email;

    //Get number of entries to add
    System.out.print("How many people do you wish to add? ");
    numFriends = Integer.parseInt(keyboard.nextLine());        //Line "25" in error

    //Prompt for info and add it to the file
    for(int i = 1; i <= numFriends; i++){
        System.out.printf("Enter the first name of person %d: ",i);
        firstName = keyboard.nextLine();
        System.out.printf("Enter the last name of person %d: ",i);
        lastName = keyboard.nextLine();
        System.out.printf("Enter the email of person %d: ",i);
        email = keyboard.nextLine();

        outputFile.println(firstName + " " + lastName + " " + email);
    }

    //Wrap things up and give a confirmation
    outputFile.close();
    System.out.println("Data written to the file.");
}