【问题标题】:Scanner using delimiter but not recognizing the integer after it扫描仪使用定界符但不识别其后的整数
【发布时间】:2019-05-05 03:33:48
【问题描述】:

我似乎无法在我的代码中找到问题,想知道你们这里的好人是否愿意帮助我。我的教授要求我们从她提供给我们的文件中提取的信息创建一个二维数组。使用 Scanner 和 File 类,我们应该能够做到这一点,但是,我遇到了减速带。在我为其设置的分隔符之后,我的扫描仪无法识别整数。这是她提供给我们的文件。

5x7
o,2,3
7,1,3
7,1,1
X,4,2

此信息由块引用中有空格的换行符分隔。

这是我的代码:

import java.io.*;
import java.util.*;
public class Battlefield {

// Use the FILL_CHAR for a space that contains no creature.
// Use these chars for creatures so that your code will pass
// the tests used for evaluating your program.
public final char FILL_CHAR = '-';
public final char OGRE      = 'o';
public final char CENTAUR   = '7';
public final char DRAGON    = 'X';

private char[][] field;

public Battlefield(String fn) {
    try {
    // You write code here.
    // Read a file and initialize the field.
    // The name of the file is passed in from the driver.
    // Keep all the file reading stuff in the try/catch block
    // to make file exceptions easier to deal with.
    File battlefield = new File(fn);
    Scanner scan = new Scanner(battlefield);
    scan.useDelimiter("x");
    int row = scan.nextInt();
    System.out.println(row);
    System.out.println(scan.next());
    System.out.println(scan.hasNextInt());
    int column = scan.nextInt();
    char[][] field = new char[row][column];

    /**
    Scanner scan2 = new Scanner(battlefield);
    scan2.useDelimiter(",");
    /**
    field[scan2.nextInt()][scan2.nextInt()] = OGRE;
    field[scan2.nextInt()][scan2.nextInt()] = CENTAUR;
    field[scan2.nextInt()][scan2.nextInt()] = CENTAUR;
    field[scan2.nextInt()][scan2.nextInt()] = DRAGON;
    **/
    } catch (IOException ex) {
        System.err.println(ex.getStackTrace());
    }
}

还有我的主要方法/驱动类:

public class BattlefieldDrv {
public static void main(String[] args)
{
    Battlefield battlefieldOne = new Battlefield("1field.dat");
    System.out.println(battlefieldOne.toString());
}
}

这是我的堆栈跟踪:

> 5
  7
  o,2,3
  7,1,3
  7,1,1
  X,4,2
false
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Unknown Source)
    at java.base/java.util.Scanner.next(Unknown Source)
    at java.base/java.util.Scanner.nextInt(Unknown Source)
    at java.base/java.util.Scanner.nextInt(Unknown Source)
    at Battlefield.<init>(Battlefield.java:38)
    at BattlefieldDrv.main(BattlefieldDrv.java:15)

感谢您提供的任何帮助或见解!

【问题讨论】:

  • 嗯,您列出的文件似乎实际上并未使用“x”作为分隔符。你为什么这样做?
  • @markspace 文件的第一行是 5x7,所以我使用“x”作为分隔符来越过 x 并到达下一个整数,即 7。
  • 您在这两个数字前面显示一个“>”和一个空格。他们怎么了?
  • 抱歉,这些不包含在文件中。我编辑了它。 @markspace
  • 好的,那么现在 7 之后的字符怎么样?

标签: java java.util.scanner delimiter java-io inputmismatchexception


【解决方案1】:

让我们单步执行这段代码。

scan.useDelimiter("x");
int row = scan.nextInt();

5 被读入row

System.out.println(row);

5 被打印出来。

System.out.println(scan.next());

文件的其余部分被读取和打印,因为那是 x 之后的内容。

System.out.println(scan.hasNextInt());

没有什么要读的了,所以NoSuchElementException被扔在这里。

您需要让扫描仪也接受换行符作为分隔符;你可以通过使用来做到这一点

scan.useDelimiter("(x|\\s)");

\\s 是“任何空格”的模式)。

作为旁注,使用try-with-resources-construct 是一种很好的做法:

try (Scanner scan = new Scanner(Paths.get("1field.dat"))) {
    scan.useDelimiter(...);
    ...
} catch (IOException e) {

这将导致您的文件资源被自动关闭。

【讨论】:

    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多