为什么我遇到 java.util.InputMismatchException 问题?
Why I get java.util.InputMismatchException problem?
There is a picture about the text file
我想在控制台中读取文件,但每次尝试都会出现错误:
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);
}
}
}
那么我应该在这段代码中修改什么?另外,我想知道我如何只阅读文本中的最后一行?
sc = new Scanner(new File("kerites.txt"));
“sc = new Scanner("kerites.txt") 表示你的扫描器资源是字符串"kerites.txt",不是文件。
There is a picture about the text file
我想在控制台中读取文件,但每次尝试都会出现错误:
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);
}
}
}
那么我应该在这段代码中修改什么?另外,我想知道我如何只阅读文本中的最后一行?
sc = new Scanner(new File("kerites.txt"));
“sc = new Scanner("kerites.txt") 表示你的扫描器资源是字符串"kerites.txt",不是文件。