繁体   English   中英

有人可以帮我弄清楚如何从“:”(不包括)读取我的文件直到行尾?

[英]Can someone help me to figure out how to read my file from ":" (not included) til the end of the line?

我的练习要求打开文件 sales.dat,并只读取每一行的数字部分。 所有这些都使用 Scanner 类

我已经尝试过 while 循环、for 循环和我书中的所有其他可能的解决方案,但都没有奏效

public static void main(String[] args) {
        String nomeFile = "sales.dat";
        Scanner inputStream = null;
        System.out.println("Il file "+ nomeFile + "\ncontiene le righe seguenti:\n");

        try {
            inputStream = new Scanner(new File(nomeFile));
            inputStream.useDelimiter(":");
            while(inputStream.hasNext()) {
                String riga = inputStream.nextLine();
                System.out.println(riga);
            }
        } catch (FileNotFoundException e) {
            System.out.println("Errore nell'apertura del file " + nomeFile);
            System.exit(0);
        }

        inputStream.close();
    }
}

预期:如果线路是San Francisco: 198870.32它必须只读取和打印198870.32

Actual: San Francisco: 198870.32
        Chicago: no report received
        New York: 298734.12

删除该行

inputStream.useDelimiter(":");

您现在可以阅读整行,然后使用split()

String riga = inputStream.nextLine(); //San Francisco: 198870.32
System.out.println(riga.split(":")[1].trim());

这应该给你198870.32

暂无
暂无

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

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