Java 从文本文件中读取行并拆分其内容

Java 从文本文件中读取行并拆分其内容,java,java.util.scanner,delimiter,readfile,Java,Java.util.scanner,Delimiter,Readfile,对于战舰游戏,我想从文本文件中读取值并将其存储到变量中。 .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 第一行表示我的板的大小 下一行的结构表示船舶,即。E它的名字和它在黑板上的坐标。例如:载波具有坐标:(3,2)、(3,3)、(3,4)、(3,5)(3,6) 与船舶相关的坐标数是固定

对于战舰游戏,我想从文本文件中读取值并将其存储到变量中。 .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
第一行表示我的板的大小

下一行的结构表示船舶,即。E它的名字和它在黑板上的坐标。例如:载波具有坐标:(3,2)、(3,3)、(3,4)、(3,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尺寸中

那么,为什么您的代码只跳过第一行,而不使用它呢

您的逻辑应该更改为添加以下内容以读取电路板的大小:

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);
}
编辑:

此字符串数组具有空值,因为每艘船的长度(#坐标)不同

问题中发布的代码中的字符串数组将不具有空值。它将只包含从每行数据中解析的数据

如果试图将数据从此数组复制到另一个固定大小的二维数组,则逻辑错误。当您知道数据的长度不同时,为什么要创建固定大小的数组

本质上,我需要将所有坐标从carrier存储到一个单独的2D数组中

我不会使用2D数组。相反,我将创建一个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 );
}
ArrayList ships=newArrayList();
while(scanner.hasNextLine())
{
...
ArrayList ship=新的ArrayList();
对于(int i=1;i
尝试以下代码:

    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();

    }
publicstaticvoidreadfile(文件f)抛出FileNotFoundException{
扫描仪=新扫描仪(f);
int lineNumber=1;
int-size_板;
int[][]板=新int[4][0];
int指数=0;
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
如果(行号==1){
line=line.replaceAll(“[\\n\\t]”,“”);
电路板尺寸=整数.parseInt(线);
系统输出打印LN(尺寸_板);
lineNumber++;
继续;
}
int[]板=新int[0];
String[]坐标=line.split(“;”);
字符串ship=坐标[0];
系统输出打印号(船舶);
int z=0;

对于(int i=1;i,这里有另一个选项供您选择。它将船舶和坐标收集到地图中,其中字符串键保存“board”或船舶名称,列表包含具有坐标或网格大小的阵列数组

publicstaticvoidreadfile(文件f)抛出FileNotFoundException{
扫描仪=新扫描仪(f);
int lineNumber=1;
Map gameData=new HashMap();
列表板=新的ArrayList();
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
如果(行号==1){
字符串[]gridSize={line};
gameData.put(“boardSize”,Arrays.asList(gridSize));
lineNumber++;
继续;
}
列表值=Arrays.asList(line.split(“;”);
列表坐标=新的ArrayList();
values.subList(1,values.size()).forEach(in->{

coordinates.add(in.split(“(?问题到底在哪里?拆分在哪里?)似乎很好,您将知道您正在解析哪一艘船。之后,您可以将coordinates[]数组中的每个字符串按*拆分,以获得各个部分(列和行?).由于我不知道您必须保存数据的数据结构,因此我无法给出更多建议。您说您“尝试创建数组int[][]“但这在您的代码中是不存在的。即使您的代码不起作用,它也应该显示您想做什么,如果需要的话,可以通过注释。请考虑:谢谢您的答复。我尝试了您的解决方案,但是,它创建了一个带有字符串的数组,其中坐标[0]包含所有船名(这很好),但坐标[1]包含所有第一个坐标。本质上,我需要将所有坐标从carrier存储到一个单独的2D数组中,战舰存储在另一个数组中,…此外,这个字符串数组有空值,因为长度(#坐标)对于每艘飞船都是不同的。谢谢你的时间,camickr!@Cedric,请参阅编辑。非常感谢。考虑到我在其他类中的逻辑是建立在二维结构上的坐标,我不得不继续使用它(因为没有时间完成任务)。谢谢你的帮助!