0

現在、ユーザーが入力した ID に基づいて行を編集できるようにするクラスを作成しています。ただし、以下のエラーが発生しています。

スレッド「メイン」の例外 java.lang.IllegalStateException: Scanner closed".

scanner.close()の下に を置いていますがint phone input。なぜこれが起こっているのかについての説明はありますか

package com.company;

import javax.sound.midi.SysexMessage;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Edit {
    static Scanner x;

    public static void editRecord() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("ENter your ID");
        String ID = scanner.nextLine();
        System.out.println("ENter your new name");
        String name = scanner.nextLine();
        System.out.println("ENter your new EMail");
        String email = scanner.nextLine();
        System.out.println("ENter your new Address");
        String address = scanner.nextLine();
        System.out.println("ENter your new Date");
        String date = scanner.nextLine();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        Date userDate = format.parse(date);
        System.out.println("ENter your Gender");
        String gender = scanner.nextLine();
        boolean Gender = Boolean.parseBoolean(gender);
        System.out.println("ENter your new phone number");
        int phone = scanner.nextInt();


        String filepath = "leads.csv";
    String tempfile = "temp.csv";
    File oldFile = new File(filepath);
    File newFile = new File(tempfile);
    String ID1 = ""; String name1 = "";String email1="";String address1 = ""; String userdate1 = "";
    String Gender1 = ""; String phone1 = "";
    scanner.close();
    try{
        FileWriter fw = new FileWriter(tempfile,true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);
        x = new Scanner(new File(filepath));
        x.useDelimiter("[,\n]");

        while (x.hasNext()){
            ID1 = x.next();
            name1 = x.next();
            phone1 = x.next();
            email1 = x.next();
            address1 = x.next();
            userdate1 = x.next();
            Gender1 = x.next();
            if(ID1.equals(ID)){
                pw.println(name+","+phone+","+email+","+address+","+userDate+","+Gender);
            }
            else {
                pw.println(name1+","+phone1+","+email1+","+address1+","+userdate1+","+Gender1);
            }
            x.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filepath);
            newFile.renameTo(dump);
        }


    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Error Occured");
    }


    }
}
4

1 に答える 1