简体   繁体   English

在扫描仪中使用定界符; 忽略空格和换行

[英]Using a delimiter in scanner; ignoring spaces & new lines

Good day, 美好的一天,

I am trying to read a txt file using scanner, and save the characters into a 2D String array. 我正在尝试使用扫描仪读取txt文件,并将字符保存到2D字符串数组中。 The txt file consists of 8 lines of characters, not separated by spaces. txt文件由8行字符组成,不能用空格分隔。 My program works (if you remove the delimiter) if they are separated by spaces, or (including the delimiter) without spaces if the characters are all in one line. 如果字符之间用空格隔开,则我的程序有效(如果您删除了分隔符),或者如果所有字符都在一行中,则我的程序可以有效(如果包括定界符)而没有空格。

If someone could show me how to save the characters correctly that would be greatly appreciated. 如果有人可以告诉我如何正确保存字符,将不胜感激。 The problem is in the Read() function, with the double for-loop. 问题出在带有双for循环的Read()函数中。 I tried adding another delimiter (s.useDelimiter(s.nextLine())) but I do not know how to implement it. 我尝试添加另一个定界符(s.useDelimiter(s.nextLine())),但我不知道如何实现它。

Keep in mind this is my first time using a scanner for a file and I'm only learning programming, thus my code won't be very advanced. 请记住,这是我第一次使用扫描仪扫描文件,并且我只是在学习编程,因此我的代码不会很高级。

Kind regards, Matthew 顺祝商祺,马修

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class BoardFile {

    String[][] board = new String[8][8];
    Scanner s;

    // Print the board
    public void Printboard() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
    }

    public void Open() {
        try {
            s = new Scanner(new File("**TXT FILE PATHNAME HERE**"));
        } catch (FileNotFoundException e) {
            System.out.println("Error");
        }
    }

    public void Read() {
        s.useDelimiter("");
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                String a = s.next();
                board[i][j] = a;
            }
        }   
    }

    public static void main(String[] args) {
        BoardFile a = new BoardFile();
        a.Open();
        a.Read();
        a.Printboard();
    }
}

The txt file: https://www.dropbox.com/s/k8fkneg2dcw0cbe/abcde.txt?dl=0 txt文件: https//www.dropbox.com/s/k8fkneg2dcw0cbe/abcde.txt? dl = 0

Use scanner's nextline() and split() function to read your result into 2d array 使用扫描仪的nextline()和split()函数将结果读取到二维数组中

public void Read() {
        for (int i = 0; i < 8; i++) {
                board[i]= s.nextLine().split("");
            }
        }   

For 2 dimensional array, you should use char[][] and not String[][] Here is the program which works. 对于二维数组,应使用char [] []而不是String [] []这是可行的程序。

package com.jvit.vm;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BoardFile {

    char[][] board = new char[8][8];
    Scanner s;

    // Print the board
    public void Printboard() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
    }

    public void Open() {
        try {
            s = new Scanner(new File("test.txt"));
        } catch (FileNotFoundException e) {
            System.out.println("Error");
        }
    }

    public void Read() {
        s.useDelimiter("\n");
        for (int i = 0; i < 8; i++) {
            char[] chars = s.next().toCharArray();
            for (int j = 0; j < 8; j++) {
                board[i][j] = chars[j];
            }
        }
    }

    public static void main(String[] args) {
        BoardFile a = new BoardFile();
        a.Open();
        a.Read();
        a.Printboard();
    }
}
public void Read() {
    String[] delimeter = { " ", "\\.", ",", "\\z"}
    for (String delimeter : delimeter)
    {
        Scanner scanner = new Scanner(args[0]);
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                String a = s.next();
                board[i][j] = a;
            }
        }   
        scanner.useDelimiter(delimeter);
        String t = scanner.next();

    }

You can use scanners skip method (as per the below) the pattern "\\n*" means one or more newline characters in the input 您可以使用扫描仪跳过方法(如下所示),模式“ \\ n *”表示输入中的一个或多个换行符

public void Read() {
    s.useDelimiter("");
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            s.skip("\n*");
            String a = s.next();
            board[i][j] = a;
        }
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM