使用Scanner(InputMismatchException)使用读取方法读取文件

问题描述 投票:1回答:1

我是Java的新手,使用扫描程序类读取文件时遇到问题。

我的目标是阅读以下.txt文件:

3
Emmalaan 23 
3051JC Rotterdam 
7 rooms 
price 300000 
Javastraat 88 
4078KB Eindhoven 
3 rooms 
price 50000 
Javastraat 93 
4078KB Eindhoven 
4 rooms 
price 55000 

文件顶部的“ 3”应作为一个整数读取,该整数表明文件有多少个房屋。 [3]之后的以下四行确定一所房子。

我尝试在类portefeuille中使用read方法读取此文件:

public static Portefeuille read(String infile)
    {
        Portefeuille returnvalue = new Portefeuille();


        try 
        {
            Scanner scan = new Scanner(new File(infile)).useDelimiter(" |/n");
            int aantalwoningen = scan.nextInt();
            for(int i = 0; i<aantalwoningen; ++i)
            {

                Woning.read(scan);  
            }

        }

        catch (FileNotFoundException e) 
        {
            System.out.println("File could not be found");
        }

        catch (IOException e) 
        {
            System.out.println("Exception while reading the file");
        }

        return returnvalue;
    }

Woning类中的read方法看起来像这样:

public static Woning read(Scanner sc)
    {


        String token_adres = sc.next();
        String token_dr = sc.next();
        String token_postcd = sc.next();
        String token_plaats = sc.next();
        int token_vraagPrijs = sc.nextInt();
        String token_kamerstxt = sc.next();
        String token_prijstxt = sc.next();
        int token_kamers = sc.nextInt();

        return new Woning(adresp, token_vraagPrijs, token_kamers);  

    }

当我尝试执行以下代码时:

Portefeuille port1 = Portefeuille.read("woningen.txt");

我收到以下错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Portefeuille.read(Portefeuille.java:48)
    at Portefeuille.main(Portefeuille.java:112)

但是,如果我使用Woning类中的read方法来读取一个字符串格式的adres:

Emmalaan 23
3051JC Rotterdam
7 Rooms 
price 300000

效果很好。

我试图将.txt文件更改为仅一个地址,但顶部没有“ 3”,因此它的格式完全正确,就像应该工作的地址一样。但是,当我从Woning类调用read方法时,它仍然给我错误。

有人可以帮我吗?

谢谢!

token readfile file-io inputmismatchexception
1个回答
0
投票

我也面临着类似的问题,所以我回答了我,以便将来对您有所帮助:

为了使此代码运行,我做了两个可能的修改。

  • 第一个选项:创建Scanner类时,将useDelimiter方法的使用更改为.useDelimiter("\\r\\n"),因为我在Windows中,因此我们可能需要\\ r才能与Windows兼容。

使用此修改,不会有例外。但是代码将再次在int token_vraagPrijs = sc.nextInt();处失败。

因为在public static Woning read(Scanner sc)中,您正在使用sc.next();。实际上,此方法从此扫描程序中查找并返回下一个完整令牌。在完整令牌之前和之后是与定界符模式匹配的输入。

因此,每个sc.next()实际上正在读取一行而不是令牌。因此,根据您的代码,sc.nextInt()尝试读取类似Javastraat 88的内容。因此,它将再次给您相同的异常。

  • 第二个选项(首选):不要使用任何定界符,Scanner类将使用默认空格,并且您的代码可以正常工作。我修改了您的代码,对我来说它正常工作。

代码:

public class Test3{                                                                                
  public static void main(String... s)
    {
    read("test.txt");
    }

public static void read(String infile)
{
    try (Scanner scan = new Scanner(new File(infile)))
    {
        int aantalwoningen = scan.nextInt();
        System.out.println(aantalwoningen);
        for (int i = 0; i < aantalwoningen; ++i)
        {
            read(scan);
        }
    }
    catch (FileNotFoundException e)
    {
        System.out.println("File could not be found");
    }
}

public static void read(Scanner sc)
{

    String token_adres = sc.next();
    String token_dr = sc.next();
    String token_postcd = sc.next();
    String token_plaats = sc.next();
    int token_vraagPrijs = sc.nextInt();
    String token_kamerstxt = sc.next();
    String token_prijstxt = sc.next();
    int token_kamers = sc.nextInt();
    System.out.println(token_adres + " " + token_dr + " " + token_postcd + " " + token_plaats + " "
        + token_vraagPrijs + " " + token_kamerstxt + " " + token_prijstxt + " " + token_kamers);

} }
© www.soinside.com 2019 - 2024. All rights reserved.