user9987783
user9987783

Reputation:

Why I get java.util.InputMismatchException problem?

There is a picture about the text file

I want to read the file in the console but every time I try it I get an error:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at feladat.feladat.main(feladat.java:26)

package feladat;

import java.util.ArrayList;
import java.util.Scanner;

class Kerites{
    int oldal;
    int hazszam;
    char szin;

    public Kerites(int oldal, int hazszam, char szin) {
        super();
        this.oldal = oldal;
        this.hazszam = hazszam;
        this.szin = szin;
    }
}

public class feladat {
     static Kerites kerites;
     static ArrayList<Kerites> keritesek = new ArrayList<>();
    public static void main(String []args) {
        Scanner sc = new Scanner("kerites.txt");

        while(sc.hasNextLine()){
            int oldal = sc.nextInt();
            int hazszam = sc.nextInt();
            char szin = sc.next().charAt(0); 


        kerites = new Kerites(oldal,
                hazszam,
                szin);
        keritesek.add(kerites);
        }
        System.out.println("A beolvasott adatok száma: " + keritesek.size());
        for (int i = 0; i < keritesek.size(); i++) {
            System.out.println(keritesek.get(i).oldal + " "
                    + keritesek.get(i).hazszam + " "
                    + keritesek.get(i).szin);
        }
    }
}

So what sould I modify in this code? Also, I would like to know that how I read only the last line in the text?

Upvotes: 0

Views: 102

Answers (1)

sishengzhang
sishengzhang

Reputation: 34

sc = new Scanner(new File("kerites.txt"));

“sc = new Scanner("kerites.txt") means that your scanner's resource is the string "kerites.txt",not a file。

Upvotes: 2

Related Questions