我正在尝试从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文件的内容:
每次你把一个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");
}