Results 1 to 3 of 3
  1. #1
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default 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)
    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.
    Java 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!
    	}}
    Last edited by Mokomi; 10-26-2011 at 08:22 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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

  3. #3
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: using a string to call a method in a different class.

    thank you! been trying to find a way. looking reflection now.

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  2. Inner class method call
    By d915172 in forum New To Java
    Replies: 3
    Last Post: 11-11-2010, 09:40 PM
  3. Trying to call a method from sub class
    By TheNewGuy in forum New To Java
    Replies: 4
    Last Post: 10-17-2010, 07:08 AM
  4. how to call class using String
    By newbiejava in forum New To Java
    Replies: 5
    Last Post: 08-03-2010, 04:20 PM
  5. Replies: 2
    Last Post: 05-01-2010, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •