使用文件 IO 并使数据以清晰的方式显示

Use File IO and make data shows in clear way

我尝试使用我为棒球队信息创建的文件 IO 声明 obj,但我不知道为什么 arrayList 一次又一次地保存相同的数据,我希望每个数据在 arraylist 中保存一次。谢谢

public static void main(String[] args) throws IOException{
    Scanner in = new Scanner(System.in);
    String fileName = "abc.txt";     //file name
    // System.out.println("Please Enter The Source File Name:: ");
    // fileName = in.nextLine();


    ArrayList<Team> group = new ArrayList<Team>();  //use arrayList save data from obj 

    File file = new File(fileName);      //open file
    Scanner scan = new Scanner(file);

    while (scan.hasNext()) {             //read file to string and split it
         String str = scan.nextLine();
         String[] arr = str.split(",");

         for (int i = 0; i < file.length(); i++) {     //use split part to declear obj"Team"
              int n = Integer.parseInt(arr[3]);
              int m = Integer.parseInt(arr[4]);
              Team tm = new Team(arr[0], arr[1], arr[2], n, m);
              group.add(tm);
         }
         //   scan.close();   
         //try to close but shows error"Scanner closed"
    }

    for(int i =0; i < group.size(); i++) {  //check arrayList work well, but fail
         System.out.println(group.get(i).getName());
    }

}

在您的代码中,for 循环内部 while 循环似乎产生了重复条目

while (scan.hasNext()) {             //read file to string and split it
            String str = scan.nextLine();
            String[] arr = str.split(",");

            int n = Integer.parseInt(arr[3]);
            int m = Integer.parseInt(arr[4]);
            Team tm = new Team(arr[0], arr[1], arr[2], n, m);
            group.add(tm);

        }

试试这个:

同时 (scan.hasNext()) {

            String str = scan.nextLine();
            String[] arr = str.split(",");

            int n = Integer.parseInt(arr[3]);
            int m = Integer.parseInt(arr[4].trim()); // Use trim()to avoid NumberFormatException                           
            Team tm = new Team(arr[0], arr[1], arr[2], n, m);
            group.add(tm);


    }
  **try this :**




import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;


    public class SureshTemp 
    {
        public static void main(String[] args) {

                    BufferedReader bf=null;

                    ArrayList al=new ArrayList();
                    try
                    {
                        String currentLine;

                        bf=new BufferedReader(new FileReader("D:\abc.txt"));

                        while((currentLine=bf.readLine())!=null)
                        {
                            System.out.println(currentLine);
                            String str=currentLine;
                            String[] arr=str.split(",");

                            int n=Integer.parseInt(arr[3]);
                            int m=Integer.parseInt(arr[4]);
                            Team t=new Team(arr[0],arr[1], arr[2], n, m);
                            al.add(t);

                        }

                    }


                    catch(Exception e)
                    {

                    e.printStackTrace();
                    }

                    finally
                    {
                        try {
                            if (bf != null)bf.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                    for(int i =0; i < al.size(); i++){        

                        System.out.println(al.get(i));
                    }

        }



    }

class Team
{
    private String First;
    private String next;
    private String third;
    private int won;
    private int lost;

    public Team(String F,String N,String T,int w,int l)
    {

    First=F;
    next=N;
    third=T;
    won=w;
    lost=l;
    }

    @Override
    public String toString() {
        return "Team [First=" + First + ", next=" + next + ", third=" + third
                + ", won=" + won + ", lost=" + lost + "]";
    }

    public String getFirst() {
        return First;
    }

    public void setFirst(String first) {
        First = first;
    }

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }

    public String getThird() {
        return third;
    }

    public void setThird(String third) {
        this.third = third;
    }

    public int getWon() {
        return won;
    }

    public void setWon(int won) {
        this.won = won;
    }

    public int getLost() {
        return lost;
    }

    public void setLost(int lost) {
        this.lost = lost;
    }

}