使用Java的扫描仪从文件中打印元素

时间:2018-09-29 08:16:03

标签: java java.util.scanner

您好,我需要Java方面的帮助。由于我一直在尝试使用Scanner从文件输出行,但我没有成功。文件内容如下所示:

from pyspark.sql import functions as F

(df.withColumn('visitdate_month', F.date_format(F.col('visitdate'), '1/M/yyyy'))
.groupBy('visitdate_month')
.agg(F.sum(F.col('visitdate_month')))
)

我试图做的第一件事是从第三行开始输出信息,并且效果很好。这是代码:

4
5
3->1->25
1->0->12
2->0->-5
0->1->0
2->1->7

}

但是当我插入值4和(第一和第二行)时,我试图弄清楚如何处理它。我做的第一件事是尝试使用if条件来查看分度符是否存在以及是否不存在:

public static void main(String[] args) throws Exception {
    Scanner scanner = null;
    BufferedReader in = null;
    String line = null;
    try{
        scanner = new Scanner(new BufferedReader(new FileReader("graphe.txt")));
        //in = new BufferedReader(new FileReader("graphe.txt"));
        scanner.useDelimiter("->");
        while (scanner.hasNextLine()){

            int value = scanner.nextInt();
            scanner.skip(scanner.delimiter());
            int value2 = scanner.nextInt();
            scanner.skip(scanner.delimiter());
            String value3 = scanner.nextLine();
            int value3Int = Integer.parseInt(value3);
            System.out.println(value + " - > " + value2 + " cost " + value3Int);
        }

    }catch (IOException e){
        e.printStackTrace();
    }finally {
        if (scanner != null){
            scanner.close();
        }
    }

}

}

但是它不能像expepeted一样工作,我的输出是这个错误

public static void main(String[] args) throws Exception {
    Scanner scanner = null;
    BufferedReader in = null;
    String line = null;
    try{
        scanner = new Scanner(new BufferedReader(new FileReader("graphe.txt")));
        //in = new BufferedReader(new FileReader("graphe.txt"));
        scanner.useDelimiter("->");
        while (scanner.hasNextLine()){

            String find = scanner.nextLine();
            if (!(find.contains("->"))){
                System.out.println(find);
            }
            else {
                int value = scanner.nextInt();
                scanner.skip(scanner.delimiter());
                int value2 = scanner.nextInt();
                scanner.skip(scanner.delimiter());
                String value3 = scanner.nextLine();
                int value3Int = Integer.parseInt(value3);
                System.out.println(value + " - > " + value2 + " cost " + value3Int);
            }
        }

    }catch (IOException e){
        e.printStackTrace();
    }finally {
        if (scanner != null){
            scanner.close();
        }
    }

}

请我知道我可以处理它,但是我一直在三思而后行,但是我不知道该怎么办。请帮助谢谢。

1 个答案:

答案 0 :(得分:1)

我认为每行正则表达式replaceAll都可以做到。

// no need to set delimiter
while (scanner.hasNextLine()) {
    String line = scanner.nextLine().replaceAll("^((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)$", 
                                                "$1 -> $2 costs $3");
    System.out.println(line);
}

如果输入与模式匹配,则将其格式化为“ x-> y cost z”,否则replaceAll将不执行任何操作,并且输出相同的行。

如果需要三个值,则可以使用Matcher.group访问捕获的值,

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    Matcher m = Pattern.compile("((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)").matcher(line);
    if (m.matches()) {
        // m.group(1), m.group(2) and m.group(3) are the three values as strings. You can convert them to ints yourself.
        System.out.println(m.group(1) + " -> " + m.group(2) + " costs " + m.group(3));
    } else {
        System.out.println(line);
    }
}