在填充的CSV文件中引发NoSuchElementException

时间:2019-09-18 23:49:07

标签: java

夫妻朋友们正在尝试在DotA中获得更多功能,因此我正在尝试编写一个程序,为每个人提供随机角色,然后记录我们的团队是赢还是输了这场比赛,然后根据角色更新每个人的胜率(有5个角色,如果我们有人在看,我也加入了“教练”,优点是不必每次比赛都坐同一个人)

我之所以选择使用Java,是因为Java是我现在在学校使用的语言,因此自然而然地我希望每个玩家都成为一个对象,并且他们所有的个人数据都与该对象一起存储,然后使用CSV表存储这些值,以便下次我运行该程序时,每个人仍然具有相同的统计信息,但这是出现问题的地方。

到目前为止,这就是我要做的,基本上只是在我开始处理其他所有事情之前,试图使对象正确运行。

main.java,player.java和data.csv是该项目的一部分。

main.java

import java.util.*;
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;

public class main {
    public static void main(String arg[]) throws IOException {
        File file = new File("data.csv");
        if(file.createNewFile()){
            System.out.println("data.csv File Created in Project root directory");
        }else System.out.println("File data.csv already exists in the project root directory");

        player[][] players = new player[15][1];
       int n = 0;
        for (int i = 0; i<14; i++)
        {       
            player player = new player();
            players[i][0]=player;
            n++;
        }

        for (int i = 0; i<n; i++)
        {
            players[i][0].getData(file);
            System.out.println(players[i][0]);
        }


    }
}

player.java

import java.util.*;
import java.util.List;

import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;

public class player {   
    String playerName;
    private int winsTotal, wins1, wins2, wins3, wins4, wins5, winsCoach, gamesTotal, games1, games2, games3, games4, games5, gamesCoach, index;
    public int flag = 0;
    public void Player() {
        winsTotal = 0;
        wins1 = 0;
        wins2 = 0;
        wins3 = 0;
        wins4 = 0;
        wins5 = 0;
        winsCoach = 0;
        gamesTotal = 0;
        games1 = 0;
        games2 = 0;
        games3 = 0;
        games4 = 0;
        games5 = 0;
        gamesCoach = 0;
        }

    public void getData(File f) throws IOException
    {
        Scanner s = new Scanner(f);
        s.useDelimiter(",");
        try {
        this.playerName = s.next();
        }
        catch (InputMismatchException e)
        {
            this.playerName = "null";
        }

        try {
        this.wins1 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.wins1 = 0;
        }

        try {
        this.wins2 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.wins2 = 0;
        }

        try {
        this.wins3 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.wins3 = 0;
        }

        try {
        this.wins4 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.wins4 = 0;
        }

        try {
        this.wins5 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.wins5 = 0;
        }

        try {
        this.winsCoach = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.winsCoach = 0;
        }

        try {
        this.winsTotal = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.winsTotal = 0;
        }

        try {
        this.gamesTotal = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.gamesTotal = 0;
        }

        try {
        this.games1 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.games1 = 0;
        }

        try {
        this.games2 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.games2 = 0;
        }

        try {
        this.games3 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.games3 = 0;
        }

        try {
        this.games4 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.games4 = 0;
        }

        try {
        this.games5 = s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.games5 = 0;
        }

        try {
        this.gamesCoach= s.nextInt();
        }
        catch (InputMismatchException e)
        {
            this.gamesCoach = 0;
        }

        s.nextLine();
    }

    public String toString()
    {
        return "Name: "+playerName+"\tWins as Carry: "+wins1+"\tWins as Mid: "+wins2+"\tWins as Offlane: "+wins3+"\tWins as 4 Support: "+wins4+"\tWins as 5 Support: "+wins5+"\tWins as Coach: "+winsCoach+"\tPlayed games: "+gamesTotal+"\tGames as Carry: "+games1+"\tGames as Mid: "+games2+"\tGames as Offlane: "+games3+"\tGames as 4 support: "+games4+"\tGames as 5 support: "+games5+"\tGames as Coach: "+gamesCoach;
    }

    }

Screenshot of data.csv

0 个答案:

没有答案