I want to read data about MMORPG characters from a .txt file and then filter on experience (minimum experience). But I'm getting this exception, which I know the meaning of but I really don't understand what I'm doing wrong. this my code:
I'm not good at java, I'm a beginner actually. Can someone please explain this to me. Probably I'm soing something very stupid :P
Karakter (Character):
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 (Characters):
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 (Main method):
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());
}
}
And this is the 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 Karakter.read(Karakter.java:38)
at Karakters.read(Karakters.java:41)
at KarakterZoeker.main(KarakterZoeker.java:14)
these lines:
Karakter kar = Karakter.read(sc); (in Karakters)
int experience = sc.nextInt(); (in Karakter)
Karakters kars = Karakters.read("infile.txt"); (in KarakterZoeker)
this is the txt file:
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