Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Method not calling?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method not calling?

    Hi, so I've been working on several simple text rpgs for a long time, and I think this one is my best yet. For some reason, my battle system only seems to work when I tried doing just a battle in the main method:

    package game;
    import java.util.*;
    public class Main {
    	public static void main(String[] args) {
    		Battle battle = new Battle();
                    Player p1 = new Player();
                    Monster m1 = new Monster();
                    battle.bMain(p1, m1, 50);
     
    	}
     
    }

    Here's what I actually want to have written in main:

    package game;
    import java.util.*;
    public class Main {
    	public static void main(String[] args) {
    		Player p1 = new Player();
    		p1.setName();
    		System.out.println("You're standing in the middle of a dirt road in the country.");
    		Scanner in = new Scanner(System.in);
    		in.nextLine();
    		System.out.println("You look around and decide to follow it south.");
    		in.nextLine();
    		System.out.println("You're not entirely sure how you got here, but you've got a strange determination to keep going.");
    		in.nextLine();
    		System.out.println("After walking for a while, you start to feel the hairs on the back of your neck rise up.");
    		in.nextLine();
    		System.out.println("Suddenly, something jumps out at you!");
    		in.nextLine();
    		Battle b1 = new Battle();
    		Monster scarecrow = new Monster();
    		scarecrow.setName("Scarecrow");
    		b1.bMain(p1, scarecrow, 50);
    		System.out.println("It appears the scarecrow has dropped something...");
    		Item lhat = new Item();
    		lhat.hMod(p1, 5);
    		in.nextLine();
    		System.out.println("It's a leather hat! It adds 5 hp.");
    		in.nextLine();
    	}
     
    }

    Here's my battle class:

    package game;
    import java.util.*;
    public class Battle {
     
    	public void bMain(Player player, Monster monster, int x) // x = experience gain from defeating monster
    	{					
    		Scanner in = new Scanner(System.in);
    		System.out.println("You've encountered a "+monster.mName);
    		player.calcAtk(); // calculate player battle statistics
    		player.calcDef();
    		player.calcHP();
    		in.nextLine();
    		while(player.hp > 0 && monster.hp > 0)
    		{
    			pTurn(player, monster);
    			if(monster.hp <= 0)
    			{
    				System.out.println(monster.mName+" has died!");
    				monster.setXP(x);
    				player.getXP(monster.xpdeath);
    				System.out.println("You've gained "+monster.xpdeath+" experience!");
    				break;
    			}
    			in.nextLine();
    			mTurn(player, monster);
    			if(player.hp <= 0)
    			{
    				System.out.println("You have died!");
    				player.isAlive = false;
    				break;
    			}
    			in.nextLine();
    		}
    	}
     
    	public void pTurn(Player player, Monster monster)
    	{
    		System.out.println("You attack!");
    		Random rand = new Random();
    		int roll = rand.nextInt(10);
    		int hchnc = roll + player.atk;
    		if(hchnc > monster.def)
    		{
    			int rolldmg = rand.nextInt(10);
    			System.out.println("You strike "+monster.mName+" for "+rolldmg+" damage");
    			monster.hp = monster.hp - rolldmg;
    			System.out.println(monster.mName+ " has " +monster.hp+ " hp.");
    		}
    		else if(hchnc <= monster.def)
    		{
    			System.out.println("You miss!");
    		}
     
     
    	}
     
    	public void mTurn(Player player, Monster monster)
    	{
    		System.out.println(monster.mName + " attacks!");
    		Random rand = new Random();
    		int roll = rand.nextInt(10);
    		int hchnc = roll + monster.atk;
    		if(hchnc > player.def)
    		{
    			int rolldmg = rand.nextInt(10) + monster.bossplus;
    			System.out.println(monster.mName+" strikes for "+rolldmg+" damage");
    			player.hp = player.hp - rolldmg;
    			System.out.println("You have "+player.hp+" hp.");
    		}
    		else if(hchnc <= player.def)
    		{
    			System.out.println(monster.mName+" misses!");
    		}
     
    	}
     
    }

    When I run the actual main method instead of the test one, it just doesn't call the battle method. It skips right to the part where the first Item is created. None of the print lines in the method happen. It's as if there were just two nextline methods from my scanner.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Method not calling?

    When I run the actual main method instead of the test one
    Which main() method are you talking about? I see two Main classes and they both have a main() method.
    One method has much fewer statements in it than the other.

    Having classes with the same name makes for confusion. Can you rename one of the classes so you can talk about the classes and their methods using unique names. Its ambiguous to talk about this Main.main() vs that Main.main().
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method not calling?

    Quote Originally Posted by Norm View Post
    Which main() method are you talking about? I see two Main classes and they both have a main() method.
    One method has much fewer statements in it than the other.

    Having classes with the same name makes for confusion. Can you rename one of the classes so you can talk about the classes and their methods using unique names. Its ambiguous to talk about this Main.main() vs that Main.main().
    I'm sorry I didn't clarify more. Main.main are both the same method in the same class, but one of them executes correctly (the one where it's basically just a test for my battle class.) and one that doesn't. (the beginning of the actual game.) I want to know why calling b1.bMain doesn't work when I'm trying to move along with my design, but it works when it's the only thing the program does.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Method not calling?

    Please rename the classes so that are not two Main classes.
    Then say which class and method doesn't do what you want it to do.


    Why not put a main() method in the Battle class and use that one main() method to start the execution of the program instead of having several Main classes?


    only seems to work
    Can you explain? What happens with one and not happen with the other?

    Post the program's output that shows what should happen
    and what does happen.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method not calling?

    They're not different classes. I wrote the first one to test the battle system I had been programming, and then replaced it with the second one...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Method not calling?

    Why not put a main() method in the Battle class and use that one main() method to start the execution of the program instead of having several Main classes?


    The posted code does not compile because of missing classes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. calling this method
    By antnas in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 01:32 PM
  2. calling a method in class
    By MrBean in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2012, 12:31 AM
  3. Calling method cant get the parameters right
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 22nd, 2011, 02:23 PM
  4. [SOLVED] method calling
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 01:43 AM
  5. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 09:24 AM

Tags for this Thread