如何扫描这一行并使用 scan.skip()?

huangapple go评论80阅读模式
英文:

How to scan this line and use scan.skip()?

问题

我有一个以以下格式的.txt文件:torch ; 3.0 ; 23.0

我想要扫描它,将第一个项目放入String中,将第二个和第三个项目放入float中。

到目前为止,我已经做了以下工作,但scan.skip(" ; ")不起作用,我也尝试过scan.skip(Pattern.compile(" ; "),但也没有起作用。

File file = new File(dir);
try {
    Scanner scan = new Scanner(file);
    scan.skip(" ; ");
    scan.useDelimiter(Pattern.compile(" ; "));
    while (scan.hasNextLine()) {
        String s = scan.next();
        float f1 = scan.nextFloat();
        float f2 = scan.nextFloat();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

您有解决方案吗?
谢谢

英文:

I have a .txt file with lines of this format example : torch ; 3.0 ; 23.0

I want to scan it in order to place the first item in a String, and the second and third in a float.

Here is what I have done so far but the scan.skip(" ; ") doesn't work, I have also tried scan.skip(Pattern.compile(" ; ") but it didn't work either.

File file = new File(dir);
    try {
        Scanner scan = new Scanner(file);
        scan.skip(" ; ");
        scan.useDelimiter(Pattern.compile(" ; "));
        while (scan.hasNextLine()) {
            String s = scan.next();
            float f1 = scan.nextFloat();
            float f2 = scan.nextFloat();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

Do you have any solutions for me ?
Thanks

答案1

得分: 2

不要更改用于读取FileScanner的分隔符。相反,实例化一个内部的Scanner来解析从文件中读取的行。此外,我会使用 try-with-Resources 来确保Scanner(和File)被关闭。就像这样:

File file = new File(dir);
try (Scanner scan = new Scanner(file)) {
    while (scan.hasNextLine()) {
        Scanner lineScan = new Scanner(scan.nextLine());
        lineScan.useDelimiter(Pattern.compile("\\s*;\\s*"));

        String s = lineScan.next();
        float f1 = lineScan.nextFloat();
        float f2 = lineScan.nextFloat();
        System.out.printf("%s %.2f %.2f%n", s, f1, f2);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
英文:

Don't change the delimiter of the Scanner you're using to read the File. Instead, instantiate an inner Scanner to parse the lines you read from the file. Aslo, I would use a try-with-Resources to make sure the Scanner (and File) are closed. Like,

File file = new File(dir);
try (Scanner scan = new Scanner(file)) {
	while (scan.hasNextLine()) {
		Scanner lineScan = new Scanner(scan.nextLine());
		lineScan.useDelimiter(Pattern.compile("\\s*;\\s*"));

		String s = lineScan.next();
		float f1 = lineScan.nextFloat();
		float f2 = lineScan.nextFloat();
		System.out.printf("%s %.2f %.2f%n", s, f1, f2);
	}
} catch (FileNotFoundException e) {
	e.printStackTrace();
}

答案2

得分: 2

以下是您要求的翻译内容:

Scanner scan = new Scanner(file);
scan.useDelimiter(Pattern.compile("[;\\n]"));
while (scan.hasNextLine()) {
    String s = scan.next();
    float f1 = Float.parseFloat(scan.next());
    float f2 = Float.parseFloat(scan.next());
    System.out.println(s + " " + f1 + " " + f2);
}
英文:

You may use ; or \n as delimiter and no need to skip, also I'd suggest to use Float.parseFloat(scan.next());

Scanner scan = new Scanner(file);
scan.useDelimiter(Pattern.compile("[;\\n]"));
while (scan.hasNextLine()) {
    String s = scan.next();
    float f1 = Float.parseFloat(scan.next());
    float f2 = Float.parseFloat(scan.next());
    System.out.println(s + " " + f1 + " " + f2);
}

答案3

得分: 2

以下是您要的翻译部分:

提供的答案在这里或者在这里可以解决您的问题。

然而,我想解释一下为什么您的代码不起作用,以及如何让它起作用。

请查看Scanner#skip(String pattern)文档,它指出:

>如果在当前位置找不到与指定模式匹配的内容,则不会跳过任何输入,而会抛出NoSuchElementException。

因此,您的scan.skip(" ; ");会阻塞线程,并继续读取标记,直到输入与指定模式(;)匹配以跳过,或抛出NoSuchElementException为止。

如果要使用.skip(..),您只需更改方法调用的顺序,如下所示:

try {
    Scanner scan = new Scanner(file);
    while (scan.hasNextLine()) {
        String s = scan.next();
        scan.skip(" ; ");
        float f1 = scan.nextFloat();
        scan.skip(" ; ");
        float f2 = scan.nextFloat();
    }
}
英文:

Answers provided here or here would solve your problem.

However, I want to explain why your code does not work, and how can you make it work.

Have a look at Scanner#skip(String pattern) documentation, which says, that:

>If a match to the specified pattern is not found at the current position, then no input is skipped and a NoSuchElementException is thrown.

So, your scan.skip(" ; "); blocks the thread, and keeps reading the tokens, until your input matches the specified pattern (;) to skip, or a NoSuchElementException is thrown.

If you want to use .skip(..), you can just change the order of your method calls, like:

try {
    Scanner scan = new Scanner(file);
    while (scan.hasNextLine()) {
        String s = scan.next();
        scan.skip(" ; ");
        float f1 = scan.nextFloat();
        scan.skip(" ; ");
        float f2 = scan.nextFloat();
    }
}

huangapple
  • 本文由 发表于 2020年10月4日 00:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64186590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定