Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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_String_Split - Fatal编程技术网

java-拆分每行未知数量的字符串

java-拆分每行未知数量的字符串,java,string,split,Java,String,Split,我有一个如下结构的文本文件: class Object0 extends Object1 class Object2 extends Object3 class Object1 class Object4 extends Object1 class Object3 我想拆分每个字符串并存储它。当每行上的字符串数已知时,我知道如何执行此操作,但是在这种情况下,给定行上可能有两个或四个单词 以下是已知字符串数时用于拆分的内容: public static v

我有一个如下结构的文本文件:

    class Object0 extends Object1
    class Object2 extends Object3
    class Object1
    class Object4 extends Object1
    class Object3
我想拆分每个字符串并存储它。当每行上的字符串数已知时,我知道如何执行此操作,但是在这种情况下,给定行上可能有两个或四个单词

以下是已知字符串数时用于拆分的内容:

public static void main(String[] args) {

        try {
            File f = new File("test.txt");
            Scanner sc = new Scanner(f);
            while(sc.hasNextLine()){
                String line = sc.nextLine();
                String[] details = line.split(" ");
                String classIdentifier = details[0];
                String classNameFirst = details[1];
             // String classExtends = details[2];
             // String classNameSecond = details[3];
            }
        } catch (FileNotFoundException e) {         
            e.printStackTrace();
        }

您可以在
details
数组上循环以获取每个拆分的字符串,无论它们有多少个。另外,我对您的
main
方法做了一些更改,使其更加正确(添加了一个finally子句来关闭
Scanner
资源)


在每次迭代中,您可以通过以下方式检查是否还有两个单词:

public static void main(String[] args) {

    try {
        File f = new File("test.txt");
        Scanner sc = new Scanner(f);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] details = line.split(" ");
            String classIdentifier = details[0];
            String classNameFirst = details[1];
            // this can be done because you declared there
            // can be only 2 or 4 words per line
            if (details.length==4) {
                String classExtends = details[2];
                String classNameSecond = details[3];
            }
            // do something useful here with extracted fields (store?)
            // before they get destroyed in the next iteration
        }
    } catch (FileNotFoundException e) {         
        e.printStackTrace();
    }
}

使用
details.length
检查是2还是4。您对
工作
代码的定义是什么?
public static void main(String[] args) {

    try {
        File f = new File("test.txt");
        Scanner sc = new Scanner(f);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] details = line.split(" ");
            String classIdentifier = details[0];
            String classNameFirst = details[1];
            // this can be done because you declared there
            // can be only 2 or 4 words per line
            if (details.length==4) {
                String classExtends = details[2];
                String classNameSecond = details[3];
            }
            // do something useful here with extracted fields (store?)
            // before they get destroyed in the next iteration
        }
    } catch (FileNotFoundException e) {         
        e.printStackTrace();
    }
}