Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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 我需要帮助解析我的输入文件,我应该为此使用arraylist吗?_Java_Arrays_Parsing_Arraylist_Input - Fatal编程技术网

Java 我需要帮助解析我的输入文件,我应该为此使用arraylist吗?

Java 我需要帮助解析我的输入文件,我应该为此使用arraylist吗?,java,arrays,parsing,arraylist,input,Java,Arrays,Parsing,Arraylist,Input,使用Java,我使用扫描仪读取输入文件,我能够读取并打印输入文件。但我现在很难解析它 示例输入文件如下所示: height 6 weight 120 name john team played for team1 start 2010 end 2012 team played for team1 start 2010 end 2012 team played for team2 start 2013 end 2015 我如何解析这些信息。当前,我正在逐行扫描输入,如: while (

使用Java,我使用扫描仪读取输入文件,我能够读取并打印输入文件。但我现在很难解析它

示例输入文件如下所示:

height 6

weight 120

name john

team played for team1 start 2010 end 2012

team played for team1 start 2010 end 2012

team played for team2 start 2013 end 2015
我如何解析这些信息。当前,我正在逐行扫描输入,如:

while (scanner.hasNext()) {
    String line = scanner.nextLine();
    System.out.println(line);
}
我会做类似的事情吗

int height = height.nextInt();
int weight = weight.nextInt();
String name = name.nextLine();

现在我只剩下最后3行了,假设前3行被正确地解析了

,正如@pshemo所说,我遗漏了添加line=line.replace(…)


我认为这个解决方案更优雅一点

顺便说一句,我真的不知道你想对这些信息做什么,所以我只是保持原样。您可以创建对象,也可以将其打印到控制台:

while (scanner.hasNext()) {

        scanner.next(); // skip "height"
        int height = scanner.nextInt(); // get height
        scanner.next(); // skip "weight"
        int weight = scanner.nextInt(); // get weight
        scanner.next(); // skip "name"
        String name = scanner.next(); // get name

        while (scanner.hasNext("team")) {
            // parse the "team info" similarly, as shown above.
            // make sure that you parse it correctly, so that
            // in the next inner while loop, the scanner.hasNext("team")
            // will be positioned just before the next line of "team"
            // or a next line that containes a new record.
        }


    }

使用时要小心。无论如何,如果不知道您想要实现什么目标,我们就无法真正帮助您。我想读取输入文件,并将我需要的每一项内容保存在它们所尊重的变量中。(身高、体重、姓名)。我想以同样的方式保存“团队为字符串开始INT结束INT而玩”的行。例如:String teamPlayed=“”;int start=“”,和int end=“”。感谢您的回复,我理解您所做的。当然是有帮助的,但是我如何去做“为……而比赛的球队”这句台词呢。记住,我可以有x数量的这些行的条目。假设我会做一个while循环?@dre你熟悉正则表达式吗?@dre如果你需要获得团队的年数,while循环会一直运行,直到从文件中读取所有数据为止,因为你可能已经注意到,常见的字符串是“团队为团队1开始”和“结束”因此,如果你将这两个字符替换掉,剩下的字符串将包含8个字符,其中前四个字符将是开始年份,后四个字符将是结束年份,我会在一分钟内用oen为团队编辑答案。我将尝试一下,并让你们知道它是如何进行的!谢谢guys@NullSaint
line.replace(“团队为之比赛)”
不会更改任何内容(字符串是不可变的,因此
replace
不会影响
line
,而是基于line创建新字符串)。您可能正在查找
line=line.replace(…)
while (scanner.hasNext()) {

        scanner.next(); // skip "height"
        int height = scanner.nextInt(); // get height
        scanner.next(); // skip "weight"
        int weight = scanner.nextInt(); // get weight
        scanner.next(); // skip "name"
        String name = scanner.next(); // get name

        while (scanner.hasNext("team")) {
            // parse the "team info" similarly, as shown above.
            // make sure that you parse it correctly, so that
            // in the next inner while loop, the scanner.hasNext("team")
            // will be positioned just before the next line of "team"
            // or a next line that containes a new record.
        }


    }