使用ArrayList Java输入和输出文件

时间:2017-11-28 11:44:34

标签: java arraylist

我有一个学校项目,我在编码部分遇到了一些问题 我希望在我输入的文件中有一种数据库。文件中的文本我想要添加到ArrayList中,之后我想添加一个新成员。添加新成员后,我希望将其添加到ArrayList并显示在输出文件中。

这是主要的:

import java.util.*;
import java.io.*;

public class Main {


    public static List<Member> membersList = new ArrayList<>();

    public static readFile file = new readFile();
    public static writeFile write = new writeFile();

    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        Main call = new Main();
        int choice;
        Scanner in = new Scanner(System.in);
        do {
            System.out.println("\n<<<< CLUB CHAIRMAN >>>>\n");

            System.out.println("1-Display members list");
            System.out.println("2-Modify a member");
            System.out.println("3-Add member");
            System.out.println("0-Exit");
            System.out.println("\n Select: ");
            choice = in.nextInt();
            in.nextLine();
            if(choice == 1)
            {
                file.openFile();
                file.readFile(membersList);
                file.closeFile();
            }
            if(choice == 2)
            {
                write.writer();
            }

            if(choice == 3)
            {
                call.addNewMember();
            }
        }while(choice != 0);
    }
    private void addNewMember() {
        int numberOfMembers;
        Scanner in = new Scanner(System.in);
        System.out.println("How many members do you want to add?");
        numberOfMembers = in.nextInt();
        in.nextLine();
        String[][] newMember = new String[numberOfMembers][4];

        for (int i = 0; i < numberOfMembers; i++)
        {
            System.out.println("\nEnter details for member " + (i + 1) + ":");
            System.out.println("Name:");
            newMember[i][0] = in.nextLine();
            System.out.println("Age:");
            newMember[i][1] = in.nextLine();
            System.out.println("Gender:");
            newMember[i][2] = in.nextLine();
            System.out.println("Experience(elite swimmer/swimming for the exercise:");
            newMember[i][3] = in.nextLine();
            System.out.println("Membership Type(passive/active):");
            newMember[i][4] = in.nextLine();

        }
        for(int i = 0; i < newMember.length; i++)
        {
            membersList.add(new Member( newMember[i][0], Integer.parseInt( newMember[i][1]),  newMember[i][2],  newMember[i][3],newMember[i][4]));
        }
        System.out.println("\nThe action completed successfully");



    }
}


class Member
{

    private String name;
    private int age;
    private String gender;
    private String experience;
    private String membershipType;

    Member(String name, int age, String gender, String experience, String membershipType)
    {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.experience = experience;
        this.membershipType = membershipType;
    }

}

填充新成员的部分后出现此错误:线程中的异常&#34; main&#34; java.lang.ArrayIndexOutOfBoundsException:4     在Main.addNewMember(Main.java:64)     在Main.main(Main.java:40)

读取文件类:

   import java.io.*;
import java.util.*;

public class readFile {

    private Scanner x ;

    public void openFile() throws FileNotFoundException {
        try {
            x = new Scanner(new File("D:\\Trial Exam\\USE CASE 1\\src\\members"));
        } catch (Exception e) {
            System.out.println("Couldn't find file");
        }
    }

    public void readFile(List<Member> list) throws FileNotFoundException {
        String fileName = "D:\\Trial Exam\\USE CASE 1\\src\\members";
        String line = null;
        Scanner x = new Scanner(new File(fileName));

        try {

            FileReader fileReader = new FileReader(fileName);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while ((line = bufferedReader.readLine()) != null) {

                String a = x.next();
                String b = x.next();
                String c = x.next();
                String d = x.next();
                String e = x.next();
                list.add(new Member(a,Integer.parseInt(b),c,d,e));
                System.out.println(line);
            }

            bufferedReader.close();
        } catch (FileNotFoundException ex) {
            System.out.println(
                    "Unable to open file '" + fileName + "'");
        } catch (IOException ex) {
            System.out.println(
                    "Error reading file '" + fileName + "'");

        }

    }
    public void closeFile() {
        x.close();
    }

}

写文件类:

 import java.io.*;

public class writeFile {
    private static String fileName = "D:\\Trial Exam\\USE CASE 1\\src\\output";

    public void writer() throws IOException {

        Main call = new Main();
        PrintWriter pw = null;
        FileOutputStream fo = null;
        File file = null;
        try {
            file = new File("D:\\Trial Exam\\USE CASE 1\\src\\output");
            pw = new PrintWriter(new FileOutputStream(file));
            fo = new FileOutputStream(file);
            int memberList = call.membersList.size();
            for (int i = 0; i < memberList; i++) {
                pw.write(call.membersList.get(i).toString() + "\n");
            }
        } finally {
            pw.flush();
            pw.close();
            fo.close();
        }
    }
}

这是成员输入文件: 会员1 27男初中被动 会员2名28名女性高级活跃 会员3 32女高级被动

1 个答案:

答案 0 :(得分:1)

您可以定义数组4的大小,因此您可以访问最大索引3。

 //Array's second dimension size is 4
 //you can access max index 3
//Example  numberOfMembers[i][3]
 String[][] newMember = new String[numberOfMembers][4];

在此行中,您访问索引4,因此您将获得ArrayIndexOutOfBoundsException。

 for (int i = 0; i < numberOfMembers; i++)
 {
      ....
     //you can access max index  newMember[i][3], not 4
     newMember[i][4] = in.nextLine();

 }