Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析具有特定文件格式的文件_Java_Parsing - Fatal编程技术网

Java 解析具有特定文件格式的文件

Java 解析具有特定文件格式的文件,java,parsing,Java,Parsing,嗨,我对java比较陌生,我想知道如何将某个文件格式解析为2D数组 文件格式由分隔值的逗号和分隔附加值集的组成 <a,b,c/><x,y,z> <... <... 任何帮助都很好,谢谢 编辑:这就是我到目前为止所拥有的 public static main (String args[]) { //Open file, read to get number of lines of file = numLine int[][] array =

嗨,我对java比较陌生,我想知道如何将某个文件格式解析为2D数组

文件格式由分隔值的逗号和分隔附加值集的<和/>组成

<a,b,c/><x,y,z>
<...
<...
任何帮助都很好,谢谢

编辑:这就是我到目前为止所拥有的

public static main (String args[])
{
    //Open file, read to get number of lines of file = numLine

    int[][] array = new int[numLine][numLine]

    for (int i = 0; i < numLine; i++)
    {
        //Unsure how to write element/line split
        array[i][i] = //input each element to array
        }
    }
}
publicstaticmain(字符串参数[])
{
//打开文件,读取以获取文件行数=numLine
int[][]数组=新int[numLine][numLine]
for(int i=0;i
正如@Young Millie所指出的,到目前为止,你尝试了什么?也就是说,您可以采取几种方法,其中之一是以下方法

一种有效的尝试是逐行读取文件,然后使用(将进一步解释)删除所有出现的符号,但您可以使用以下替换:

String line = "<a,b,c/><x,y,z>";
line = line.replaceAll("[<>]", "");
System.out.println("1. " + line);
然后我们在“/”上拆分字符串,得到两个所需字符串的数组:

String[] lines = line.split("/");
System.out.println("1. " + lines[0]);
System.out.println("2. " + lines[1]);
结果:

1. a,b,c
2. x,y,z

您可以根据自己的需要对此进行修改。我添加了一些评论,因此您可能需要注意它们

    Scanner sc = new Scanner(file);

    String[][] array = new String[numLine][numLine];//declaring the matrix

    int r=0 , c=0;//declaring the index of the matrices column and row

    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        line = line.replaceAll("[<>]", "");//removing > and < so we gonna have a,b,c/x,y,z
        String[] col = line.split("/");// spliting using / and we gonna have  a,b,c    x,y,z

        for (String row : col) {
            //a,b,c or x,y,z
            String[] oneCol = row.split(",");
            for (String oneRow : oneCol) {
                if(c >= numLine){
                    c = 0;
                    break;
                }
                array[r][c] = oneRow;
                c++;
            }
            r++;
            //System.out.println();
        }
        c = 0;

    }

    sc.close();
Scanner sc=新扫描仪(文件);
字符串[][]数组=新字符串[numLine][numLine]//声明矩阵
int r=0,c=0//声明矩阵列和行的索引
while(sc.hasNextLine()){
字符串行=sc.nextLine();
line=line.replaceAll(“[]”,“”);//删除>和<所以我们有a,b,c/x,y,z
String[]col=line.split(“/”);//使用/进行拆分,得到a,b,cx,y,z
for(字符串行:列){
//a、 b,c或x,y,z
字符串[]oneCol=row.split(“,”);
for(字符串oneRow:oneCol){
如果(c>=numLine){
c=0;
打破
}
数组[r][c]=oneRow;
C++;
}
r++;
//System.out.println();
}
c=0;
}
sc.close();

到目前为止,你写了什么?投票决定结束,因为范围太广。有几十种方法可以解决这个问题。大多数Java入门书籍最多在前五章中提供两种合适的方法。
1. a,b,c
2. x,y,z
    Scanner sc = new Scanner(file);

    String[][] array = new String[numLine][numLine];//declaring the matrix

    int r=0 , c=0;//declaring the index of the matrices column and row

    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        line = line.replaceAll("[<>]", "");//removing > and < so we gonna have a,b,c/x,y,z
        String[] col = line.split("/");// spliting using / and we gonna have  a,b,c    x,y,z

        for (String row : col) {
            //a,b,c or x,y,z
            String[] oneCol = row.split(",");
            for (String oneRow : oneCol) {
                if(c >= numLine){
                    c = 0;
                    break;
                }
                array[r][c] = oneRow;
                c++;
            }
            r++;
            //System.out.println();
        }
        c = 0;

    }

    sc.close();