gpt4 book ai didi

java - 输入不匹配异常

转载 作者:行者123 更新时间:2023-11-30 05:15:18 25 4
gpt4 key购买 nike

我想从 .txt 文件中读取有关 MMORPG 角色的数据,然后根据经验进行筛选(最低经验)。但我遇到了这个异常,我知道它的含义,但我真的不明白我做错了什么。

我不擅长java,实际上我是一个初学者。有人可以向我解释一下吗?也许我正在做一些非常愚蠢的事情。

这是我的代码:

Karakter(角色):

import java.util.Scanner;


public class Karakter {

private String name;
private int experience;
private int maxHealthPoints;
private int healthPoints;
private int maxGreed;
private int greed;

public Karakter(String nm, int exp, int mHP, int hp, int mG, int g)
{
name = nm;
experience = exp;
maxHealthPoints = mHP;
healthPoints = hp;
maxGreed = mG;
greed = g;
}

public String toString()
{
String s = "";
s += getName();
s += getExperience();
s += getMaxHealthPoints();
s += getHealthPoints();
s += getMaxGreed();
s += getGreed();
return s;
}

public static Karakter read(Scanner sc)
{
String name = sc.next();
int experience = sc.nextInt();
int maxHealthPoints = sc.nextInt();
int healthPoints = sc.nextInt();
int maxGreed = sc.nextInt();
int greed = sc.nextInt();
return new Karakter(name, experience, maxHealthPoints, healthPoints, maxGreed, greed);
}

public boolean hasExperience(int min)
{
return experience >= min;
}
// returns true if Krakters have the same name
public boolean equals(Object other)
{
if(!(other instanceof Karakter))
{
return false;
}
else
{
Karakter that = (Karakter) other;
return that.name == this.name;
}
}

public String getName()
{
return name;
}

public int getExperience()
{
return experience;
}

public int getMaxHealthPoints()
{
return maxHealthPoints;
}

public int getHealthPoints()
{
return healthPoints;
}

public int getMaxGreed()
{
return maxGreed;
}

public int getGreed()
{
return greed;
}


}

Karakters(角色):

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Karakters {



private ArrayList<Karakter> kars = new ArrayList<Karakter>();

public void voegToe(Karakter kar)
{
if(!kars.contains(kar))
{
kars.add(kar);
}
}

// returns an arraylist of characters with exp >= minexp

public ArrayList<Karakter> karaktersVanaf(int minExperience)
{
Karakter kar = null;
for(int i = 0; i < kars.size(); i++)
{
if(kar.hasExperience(minExperience))
kars.add(kar);
}
return kars;
}

public static Karakters read(String infile)
{
Karakters k = new Karakters();
try
{
FileReader fr = new FileReader(infile);
Scanner sc = new Scanner(fr);
int aantal = sc.nextInt();
for(int i = 0; i < aantal ; i++)
{
Karakter kar = Karakter.read(sc);
k.kars.add(kar);
}
fr.close();
}
catch(IOException iox)
{
System.out.println(iox);
return null;
}
return k;
}

}

KarakterZoeker(主要方法):

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


public class KarakterZoeker {


public static void main(String[] args)
{


Scanner sc = new Scanner(System.in);
Karakters kars = Karakters.read("infile.txt");

System.out.println("Welke experience wilt u minimaal?");
int minExp = sc.nextInt();

ArrayList<Karakter> s = kars.karaktersVanaf(minExp);

System.out.println(s.toString());

}

}

这是错误:

 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 Karakter.read(Karakter.java:38)
at Karakters.read(Karakters.java:41)
at KarakterZoeker.main(KarakterZoeker.java:14)

这些行:

Karakter kar = Karakter.read(sc);  (in Karakters)
int experience = sc.nextInt(); (in Karakter)
Karakters kars = Karakters.read("infile.txt"); (in KarakterZoeker)

这是txt文件:

2
name = Dursley
experience = 15
maxHealthPoints = 20
healthPoints = 10
maxGreed = 3
greed = 1
name = Aragorn
experience = 45
maxHealthPoints = 40
healthPoints = 30
maxGreed = 20
greed = 10

最佳答案

InputMismatchException 由扫描器抛出,表示检索到的 token 与预期类型的​​模式不匹配,或者 token 超出预期类型的​​范围

所以,我的猜测是,当下一个标记实际上不是 Integer 时,您正在调用 nextInt

编辑

阅读该文件时需要小心,因为您的标签被视为标记。例如,当您有:

maxHealthPoints = 20 ,

您有三个 token :“maxHealthPoints”、“=”和“20”。问题是您忽略了标签(“=”之前)和等号。我的建议?逐行读取文件并使用扫描仪解析每一行。或者,从文件中删除标签和等号,仅保留要提取的值。

关于java - 输入不匹配异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1652671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com