问题描述
对于战舰游戏,我想从文本文件中读取值并将它们存储到变量中。 .txt 文件示例:
8
Carrier;3*2;3*3;3*4;3*5;3*6
Battleship;5*6;6*6;7*6;8*6
Submarine;5*2;6*2;7*2;
Destroyer;1*7;1*8
第一行表示我的板子的大小。
下一行的结构表示船,i。 e.它的名称以及它在板上的坐标。例如:Carrier 有坐标:(3,2),(3,3),4),5)(3,6)。
与船舶相关的坐标数量是固定的。但是,船舶所在的线路可能会发生变化。
现在,我尝试创建一个名为 Carrier 的数组 int[][],其中 int[0][0] 为 3,int[0][1] 为 2,...,并执行此操作每艘船。
之后,总是放在第一行的棋盘尺寸应该存储在变量 int size;
中。
public void ReadFile(File f) throws FileNotFoundException {
Scanner scanner = new Scanner(f);
int lineNumber = 1;
while(scanner.hasNextLine()){
String line = scanner.nextLine();
if(lineNumber==1){ // Skipping board size for Now.
lineNumber++;
continue;
}
String[] coordinates = line.split(";");
String ship = coordinates[0];
System.out.println(ship);
lineNumber++;
}
scanner.close();
}
我尝试使用分隔符、拆分等,但我没有找到解决方案。感谢您的帮助!
解决方法
同样,总是放在第一行的棋盘大小应该存储在一个变量 int size 中;
那么为什么你的代码只是跳过第一行而不做任何处理?
您的逻辑应该更改为添加类似以下内容以读取板的大小:
int size = scanner.nextInt();
scanner.nextLine(); // to skip to the next line of data
然后您使用循环读取所有船舶信息:
while(scanner.hasNextLine()){
String line = scanner.nextLine();
String[] coordinates = line.split(";");
String ship = coordinates[0];
System.out.println(ship);
}
编辑:
这个字符串数组有空值,因为每艘船的长度(#coordinates)是不同的。
您问题中发布的代码中的字符串数组不会有空值。它只会包含从每一行数据中解析出来的数据。
如果您尝试将此数组中的数据复制到另一个固定大小的二维数组,那么您的逻辑是错误的。明明数据长度不同,为什么还要创建固定大小的数组?
本质上,我需要将来自载体的所有坐标存储到一个单独的二维数组中
我不会使用二维数组。相反,我会创建一个 ArrayList,其中包含一艘船的所有坐标。然后你需要一个 ArrayList 来保存所有的船。所以逻辑应该是这样的:
ArrayList<ArrayList<Point>> ships = new ArrayList<>();
while(scanner.hasNextLine())
{
...
ArrayList<Point> ship = new ArrayList<>();
for (int i = 1; i < coordinates.length; i++)
{
// split the value in the coordinates array at the given index
// use the two values to create a Point object
Point point = new Point(...);
ship.add( point );
}
ships.add( ship );
}
,
试试这个代码:
public static void ReadFile(File f) throws FileNotFoundException {
Scanner scanner = new Scanner(f);
int lineNumber = 1;
int size_board;
int [][] boards=new int[4][0];
int index=0;
while(scanner.hasNextLine()){
String line = scanner.nextLine();
if(lineNumber==1){
line=line.replaceAll("[\\n\\t ]","");
size_board=Integer.parseInt(line);
System.out.println(size_board);
lineNumber++;
continue;
}
int[] board=new int[0];
String[] coordinates = line.split(";");
String ship = coordinates[0];
System.out.println(ship);
int z=0;
for (int i=1;i<coordinates.length ; i++) {
board = Arrays.copyOf(board,board.length+2);
String[] coords=coordinates[i].split("\\*");
board[z++]=Integer.parseInt(coords[0]);
board[z++]=Integer.parseInt(coords[1]);
}
lineNumber++;
boards[index]=Arrays.copyOf(boards[index],board.length);
boards[index++]=board;
}
scanner.close();
}
,
这是您的另一种选择。这会将船只和坐标收集到 Map
public static void ReadFile(File f) throws FileNotFoundException {
Scanner scanner = new Scanner(f);
int lineNumber = 1;
Map<String,List> gameData = new HashMap<>();
List board = new ArrayList();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (lineNumber == 1) {
String[] gridSize = { line };
gameData.put("boardSize",Arrays.asList(gridSize));
lineNumber++;
continue;
}
List<String> values = Arrays.asList(line.split(";"));
List<String[]> coordinates = new ArrayList();
values.subList(1,values.size()).forEach(in -> {
coordinates.add(in.split("(?<![*])[*](?![*])"));
});
gameData.put(values.get(0),coordinates);
lineNumber++;
}
gameData.forEach((k,v) -> {
System.out.print(k);
System.out.println(Arrays.deepToString(v.toArray()));
});
scanner.close();
}
}