2

I'm new to java and I have a problem with reading a file using the scanner class.

My objective is to read the following .txt file:

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 

The "3" on top of the file should be read as an integer that tells how many houses the file has. The following four lines after the "3" determine one house.

I try to read this file using a read method in the class portefeuille:

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;
    }

The read method in the Woning class looks like this:

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);  

    }

When I try to execute the following code:

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

I get the following error:

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)

However if I use the read method from the Woning class to read one adres in a string format:

Emmalaan 23
3051JC Rotterdam
7 Rooms 
price 300000

It works fine.

I tried to change the .txt file into only one address without the "3" on top so that it is exactly formatted like the address that should work. But when I call the read method from Woning class it still gives me the error.

Could anyone please help me with this?

Thank you!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kien
  • 23
  • 3

1 Answers1

0

I was also facing a similar issue, so I put my answer so that it could help in future:

There are two possible modifications which I did to make this code run.

  • First option: Change the use of useDelimiter method to .useDelimiter("\\r\\n") when creating the Scanner class, I was in windows so we might need \\r for Windows compatibility.

Using this modification, there will be no exception.But the code will again fail at int token_vraagPrijs = sc.nextInt();.

Because in the public static Woning read(Scanner sc), you are suing sc.next();.Actually this method finds and returns the next complete token from this scanner.A complete token is preceded and followed by input that matches the delimiter pattern.

So, every sc.next() is actually reading a line not a token. So as per your code sc.nextInt() is trying to read something like Javastraat 88.So again it will give you the same exception.

  • Second option (Preferred):Don't use any delimiter, Scanner class will default whitespace and your code will work fine.I modified your code and It worked fine for me.

Code:

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);

} }
Deep
  • 929
  • 2
  • 16
  • 32