如何使用我的主类修复我的loadData方法

问题描述 投票:1回答:1

我正在尝试从txt文件加载数据,它只会读取txt文件的一行。当我在loadData方法中指定我的for循环中的int I变量时,它将打印该特定行。我不确定为什么它不会只添加和打印我的所有数据。

我尝试使用外部for循环来查看是否会打印并以这种方式添加数据,但没有运气

import java.io.*;
import java.util.*;
public class BingoSortTest
{
static BingoPlayer [] test;
public static void main (String [] args) throws IOException
{
        Scanner keyboard = new Scanner(System.in);
        test = new BingoPlayer [10];
        loadData();

            System.out.print(Arrays.toString(test));


}
public static void loadData() throws IOException    
{
    Scanner S = new Scanner(new FileInputStream("players.txt"));
    double houseMoney = S.nextDouble();
    S.nextLine();
    int player = S.nextInt();
    S.nextLine();

        for(int i = 0; i < test.length; i++)
        {
            String line = S.nextLine();
            String [] combo = line.split(",");
            String first = combo [0];
            String last = combo [1];
            double playerMoney = Double.parseDouble(combo[2]);
            BingoPlayer plays = new BingoPlayer(first, last, playerMoney);
            add(plays);
        }

}
public static void add(BingoPlayer d)
{
    int count = 0;
    if (count< test.length)
    {
        test[count] = d;
        count++;
    }
    else
        System.out.println("No room");
 }
}

这是我正在使用的txt文件的内容:

  • 50.00
  • 10
  • 詹姆斯·史密斯,50.0
  • 迈克尔·史密斯,50.0
  • 罗伯特·史密斯,50.0
  • 玛丽亚,加西亚,50.0
  • 大卫·史密斯,50.0
  • Maria,Rodriguez,50.0
  • 玛丽·史密斯,50.0
  • Maria,Hernandez,50.0
  • Maria,Martinez,50.0
  • 詹姆斯,克拉珀,50.0
java
1个回答
1
投票

每次你把一个BingoPlayer放在Index 0

public static void add(BingoPlayer d)
{
    int count = 0; // <-------------------- Here
    if (count< test.length)
    {
        test[count] = d;
        count++;
    }
    else
        System.out.println("No room");
 }

你必须定义静态计数器变量,其中定义了BingoPlayer数组。

define count变量static

static BingoPlayer [] test;
static int count = 0;

并像这样更改添加函数定义。

public static void add(BingoPlayer d)
{
    if (count< test.length)   {
        test[count] = d;
        count++;
    }
    else
        System.out.println("No room");
 }
© www.soinside.com 2019 - 2024. All rights reserved.