using a string to call a method in a different class.
this is the file(or charatersheet) (i've tried BufferedReader, but the only problem is i can only read lines into a string instead of just the next word)
Quote:
Owner: presti
Level: 23
Race: Human
Age: 53
Health: 2760
Max Hp: 2760
mana: 1840
Max Mana: 1840
statsStart
Power: 181.0
Cooldowns: 247.0
Abilities: 219.0
Longagevity: 153.0
statsEnd
Cords: (-90.0,225.0)
inventoryStart
1: Empty
2: Empty
3: Empty
4: Empty
5: Empty
6: Empty
7: Empty
8: Empty
9: Empty
10: Empty
inventoryEnd
abilitiesStart
nuke
300
I'm attempting to use Scanner and pick up a spesific line.(nuke) and then have convert it to a string.(test=he.next();) from there go to the abilities class and go to the nuke method.
I could use a case switch method. but every time I would like to add a new ability I have to add a new case leading to the new ability method. trying to find a way to have it just read "nuke" and then go find the "nuke" method without so much added work.
(this is important because i'm going to do an int and more string array's to transfer over each calling different methods. like for example
nuke then adds a slow to it. i'm going to have to do a case switch statement for every ability.)
silly me forgot to add the code.
Code:
public class abl {
public static void abilitys() throws IOException{
Scanner he = new Scanner(new File("zren.txt"));
//BufferedReader ability = new BufferedReader(new FileReader("zren.txt"));
String test="",start="abilitiesStart",foo;
int res=0,set=0;
do{
test= he.next();
// to make sure it is doing the do while correctly. System.out.println(test);
}
while(!test.equals(start));
System.out.println(res);
foo=he.next();
//picks up nuke
res=he.nextInt();
// to what the Int and the string i copied. seeSystem.out.println("test "+test+" res "+res);
he.close();
abilitiesList attack = new abilitiesList();
//now i want to go to the nuke function
attack.foo();
//sadily foo isn't want i want. it's the string inside of foo I want!
}}
Re: using a string to call a method in a different class.
Reflection is the keyword: Using Java Reflection
But it sounds a little weird for me :D
Re: using a string to call a method in a different class.
thank you! been trying to find a way. looking reflection now.