Java cannot find a specific method on a seperate file












0














Im using Visual Studio Code, and I just create a folder with the "project" inside, i dont like to use netbeans, eclipse, etc for small programs.



So I'm creating a small program, that i doubted would work, but it did for the mos part, it creates a rpg like character and a 2nd one and creates a simulated battle in which one character wins based on one stat. Everything seems to work but when I call the method Battle(oumar, aisha);, which takes two characters and has them battle, it gets an error saying
Main.java:6: error: cannot find symbol
Battle(oumar, aisha);
^
symbol: method Battle(Character,Character)
location: class Main



So since im not so experienced im not sure what the problem really is.
Here is the code for the main class:



    public class Main {
public static void main(String args) {

Character oumar = new Character("Oumar", 10);
Character aisha = new Character("Aisha", 9);
Battle(oumar, aisha);

}


}


and here is the second class within a seperate file (still in the same folder)



public class Character {

String name;
int BattlePower;
int wins;
Character one;
Character two;

public Character(String name, int BattlePower) {
this.name = name;
this.wins = wins;
System.out.println("New character: "+ name);
this.BattlePower = BattlePower;
this.wins = wins;
System.out.println(name + "has a Battle Power of " + BattlePower);
}

public void Battle(Character one, Character two) {

this.one = one;
this.two = two;

if (one.BattlePower > two.BattlePower ) {
System.out.print("Character " + one + " has won the Battle!");
one.wins++;
System.out.print("Character one now has " + wins + " wins!");
}
else if (two.BattlePower > one.BattlePower) {
System.out.print("Character " + two + " has won the Battle!");
two.wins++;
System.out.print("Character two now has " + wins + " wins!");
}
else {

System.out.print("The two characters have tied!");

}
}
}


Any help would be great, as well as any tips that might help me in the future.










share|improve this question



























    0














    Im using Visual Studio Code, and I just create a folder with the "project" inside, i dont like to use netbeans, eclipse, etc for small programs.



    So I'm creating a small program, that i doubted would work, but it did for the mos part, it creates a rpg like character and a 2nd one and creates a simulated battle in which one character wins based on one stat. Everything seems to work but when I call the method Battle(oumar, aisha);, which takes two characters and has them battle, it gets an error saying
    Main.java:6: error: cannot find symbol
    Battle(oumar, aisha);
    ^
    symbol: method Battle(Character,Character)
    location: class Main



    So since im not so experienced im not sure what the problem really is.
    Here is the code for the main class:



        public class Main {
    public static void main(String args) {

    Character oumar = new Character("Oumar", 10);
    Character aisha = new Character("Aisha", 9);
    Battle(oumar, aisha);

    }


    }


    and here is the second class within a seperate file (still in the same folder)



    public class Character {

    String name;
    int BattlePower;
    int wins;
    Character one;
    Character two;

    public Character(String name, int BattlePower) {
    this.name = name;
    this.wins = wins;
    System.out.println("New character: "+ name);
    this.BattlePower = BattlePower;
    this.wins = wins;
    System.out.println(name + "has a Battle Power of " + BattlePower);
    }

    public void Battle(Character one, Character two) {

    this.one = one;
    this.two = two;

    if (one.BattlePower > two.BattlePower ) {
    System.out.print("Character " + one + " has won the Battle!");
    one.wins++;
    System.out.print("Character one now has " + wins + " wins!");
    }
    else if (two.BattlePower > one.BattlePower) {
    System.out.print("Character " + two + " has won the Battle!");
    two.wins++;
    System.out.print("Character two now has " + wins + " wins!");
    }
    else {

    System.out.print("The two characters have tied!");

    }
    }
    }


    Any help would be great, as well as any tips that might help me in the future.










    share|improve this question

























      0












      0








      0







      Im using Visual Studio Code, and I just create a folder with the "project" inside, i dont like to use netbeans, eclipse, etc for small programs.



      So I'm creating a small program, that i doubted would work, but it did for the mos part, it creates a rpg like character and a 2nd one and creates a simulated battle in which one character wins based on one stat. Everything seems to work but when I call the method Battle(oumar, aisha);, which takes two characters and has them battle, it gets an error saying
      Main.java:6: error: cannot find symbol
      Battle(oumar, aisha);
      ^
      symbol: method Battle(Character,Character)
      location: class Main



      So since im not so experienced im not sure what the problem really is.
      Here is the code for the main class:



          public class Main {
      public static void main(String args) {

      Character oumar = new Character("Oumar", 10);
      Character aisha = new Character("Aisha", 9);
      Battle(oumar, aisha);

      }


      }


      and here is the second class within a seperate file (still in the same folder)



      public class Character {

      String name;
      int BattlePower;
      int wins;
      Character one;
      Character two;

      public Character(String name, int BattlePower) {
      this.name = name;
      this.wins = wins;
      System.out.println("New character: "+ name);
      this.BattlePower = BattlePower;
      this.wins = wins;
      System.out.println(name + "has a Battle Power of " + BattlePower);
      }

      public void Battle(Character one, Character two) {

      this.one = one;
      this.two = two;

      if (one.BattlePower > two.BattlePower ) {
      System.out.print("Character " + one + " has won the Battle!");
      one.wins++;
      System.out.print("Character one now has " + wins + " wins!");
      }
      else if (two.BattlePower > one.BattlePower) {
      System.out.print("Character " + two + " has won the Battle!");
      two.wins++;
      System.out.print("Character two now has " + wins + " wins!");
      }
      else {

      System.out.print("The two characters have tied!");

      }
      }
      }


      Any help would be great, as well as any tips that might help me in the future.










      share|improve this question













      Im using Visual Studio Code, and I just create a folder with the "project" inside, i dont like to use netbeans, eclipse, etc for small programs.



      So I'm creating a small program, that i doubted would work, but it did for the mos part, it creates a rpg like character and a 2nd one and creates a simulated battle in which one character wins based on one stat. Everything seems to work but when I call the method Battle(oumar, aisha);, which takes two characters and has them battle, it gets an error saying
      Main.java:6: error: cannot find symbol
      Battle(oumar, aisha);
      ^
      symbol: method Battle(Character,Character)
      location: class Main



      So since im not so experienced im not sure what the problem really is.
      Here is the code for the main class:



          public class Main {
      public static void main(String args) {

      Character oumar = new Character("Oumar", 10);
      Character aisha = new Character("Aisha", 9);
      Battle(oumar, aisha);

      }


      }


      and here is the second class within a seperate file (still in the same folder)



      public class Character {

      String name;
      int BattlePower;
      int wins;
      Character one;
      Character two;

      public Character(String name, int BattlePower) {
      this.name = name;
      this.wins = wins;
      System.out.println("New character: "+ name);
      this.BattlePower = BattlePower;
      this.wins = wins;
      System.out.println(name + "has a Battle Power of " + BattlePower);
      }

      public void Battle(Character one, Character two) {

      this.one = one;
      this.two = two;

      if (one.BattlePower > two.BattlePower ) {
      System.out.print("Character " + one + " has won the Battle!");
      one.wins++;
      System.out.print("Character one now has " + wins + " wins!");
      }
      else if (two.BattlePower > one.BattlePower) {
      System.out.print("Character " + two + " has won the Battle!");
      two.wins++;
      System.out.print("Character two now has " + wins + " wins!");
      }
      else {

      System.out.print("The two characters have tied!");

      }
      }
      }


      Any help would be great, as well as any tips that might help me in the future.







      java visual-studio oop object






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 3:22









      oumar diop

      62




      62
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I believe you would need to use



          oumar.Battle(oumar, aisha)



          or



          aisha.Battle(oumar, aisha)



          as the method is inside of a non static class so it can't be called without an object reference.



          If you want to call it like



          Battle(oumar, aisha)



          you'll need to move it to the main class.



          Also, this issue should be present on those other IDEs as well.






          share|improve this answer





















          • Thanks very much i feel like an idiot, of course a method within an object class needs to be called by the object (not sure if i worded that properly) but thank you
            – oumar diop
            Nov 23 '18 at 20:13



















          0














          You can put a no-arg constructor inside the Character class like public Character(){}. Then in the Main.java you can use the no-arg constructor to create an object of the Character class and then invoke the method Battle like Character ch1 = new Character(); ch1.Battle(oumar,aisha).






          share|improve this answer





















          • Thanks this helped, but im just wondering what would i put inside of this constructor, and do i keep the original one?
            – oumar diop
            Nov 23 '18 at 20:14










          • You have to put nothing inside the constructor and you can keep the original one. This is constructor overload ing feature of java.
            – Priyadeep Datta
            Nov 23 '18 at 23:51











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440286%2fjava-cannot-find-a-specific-method-on-a-seperate-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I believe you would need to use



          oumar.Battle(oumar, aisha)



          or



          aisha.Battle(oumar, aisha)



          as the method is inside of a non static class so it can't be called without an object reference.



          If you want to call it like



          Battle(oumar, aisha)



          you'll need to move it to the main class.



          Also, this issue should be present on those other IDEs as well.






          share|improve this answer





















          • Thanks very much i feel like an idiot, of course a method within an object class needs to be called by the object (not sure if i worded that properly) but thank you
            – oumar diop
            Nov 23 '18 at 20:13
















          0














          I believe you would need to use



          oumar.Battle(oumar, aisha)



          or



          aisha.Battle(oumar, aisha)



          as the method is inside of a non static class so it can't be called without an object reference.



          If you want to call it like



          Battle(oumar, aisha)



          you'll need to move it to the main class.



          Also, this issue should be present on those other IDEs as well.






          share|improve this answer





















          • Thanks very much i feel like an idiot, of course a method within an object class needs to be called by the object (not sure if i worded that properly) but thank you
            – oumar diop
            Nov 23 '18 at 20:13














          0












          0








          0






          I believe you would need to use



          oumar.Battle(oumar, aisha)



          or



          aisha.Battle(oumar, aisha)



          as the method is inside of a non static class so it can't be called without an object reference.



          If you want to call it like



          Battle(oumar, aisha)



          you'll need to move it to the main class.



          Also, this issue should be present on those other IDEs as well.






          share|improve this answer












          I believe you would need to use



          oumar.Battle(oumar, aisha)



          or



          aisha.Battle(oumar, aisha)



          as the method is inside of a non static class so it can't be called without an object reference.



          If you want to call it like



          Battle(oumar, aisha)



          you'll need to move it to the main class.



          Also, this issue should be present on those other IDEs as well.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 3:38









          Jonathan Van Dam

          351315




          351315












          • Thanks very much i feel like an idiot, of course a method within an object class needs to be called by the object (not sure if i worded that properly) but thank you
            – oumar diop
            Nov 23 '18 at 20:13


















          • Thanks very much i feel like an idiot, of course a method within an object class needs to be called by the object (not sure if i worded that properly) but thank you
            – oumar diop
            Nov 23 '18 at 20:13
















          Thanks very much i feel like an idiot, of course a method within an object class needs to be called by the object (not sure if i worded that properly) but thank you
          – oumar diop
          Nov 23 '18 at 20:13




          Thanks very much i feel like an idiot, of course a method within an object class needs to be called by the object (not sure if i worded that properly) but thank you
          – oumar diop
          Nov 23 '18 at 20:13













          0














          You can put a no-arg constructor inside the Character class like public Character(){}. Then in the Main.java you can use the no-arg constructor to create an object of the Character class and then invoke the method Battle like Character ch1 = new Character(); ch1.Battle(oumar,aisha).






          share|improve this answer





















          • Thanks this helped, but im just wondering what would i put inside of this constructor, and do i keep the original one?
            – oumar diop
            Nov 23 '18 at 20:14










          • You have to put nothing inside the constructor and you can keep the original one. This is constructor overload ing feature of java.
            – Priyadeep Datta
            Nov 23 '18 at 23:51
















          0














          You can put a no-arg constructor inside the Character class like public Character(){}. Then in the Main.java you can use the no-arg constructor to create an object of the Character class and then invoke the method Battle like Character ch1 = new Character(); ch1.Battle(oumar,aisha).






          share|improve this answer





















          • Thanks this helped, but im just wondering what would i put inside of this constructor, and do i keep the original one?
            – oumar diop
            Nov 23 '18 at 20:14










          • You have to put nothing inside the constructor and you can keep the original one. This is constructor overload ing feature of java.
            – Priyadeep Datta
            Nov 23 '18 at 23:51














          0












          0








          0






          You can put a no-arg constructor inside the Character class like public Character(){}. Then in the Main.java you can use the no-arg constructor to create an object of the Character class and then invoke the method Battle like Character ch1 = new Character(); ch1.Battle(oumar,aisha).






          share|improve this answer












          You can put a no-arg constructor inside the Character class like public Character(){}. Then in the Main.java you can use the no-arg constructor to create an object of the Character class and then invoke the method Battle like Character ch1 = new Character(); ch1.Battle(oumar,aisha).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 3:53









          Priyadeep Datta

          192




          192












          • Thanks this helped, but im just wondering what would i put inside of this constructor, and do i keep the original one?
            – oumar diop
            Nov 23 '18 at 20:14










          • You have to put nothing inside the constructor and you can keep the original one. This is constructor overload ing feature of java.
            – Priyadeep Datta
            Nov 23 '18 at 23:51


















          • Thanks this helped, but im just wondering what would i put inside of this constructor, and do i keep the original one?
            – oumar diop
            Nov 23 '18 at 20:14










          • You have to put nothing inside the constructor and you can keep the original one. This is constructor overload ing feature of java.
            – Priyadeep Datta
            Nov 23 '18 at 23:51
















          Thanks this helped, but im just wondering what would i put inside of this constructor, and do i keep the original one?
          – oumar diop
          Nov 23 '18 at 20:14




          Thanks this helped, but im just wondering what would i put inside of this constructor, and do i keep the original one?
          – oumar diop
          Nov 23 '18 at 20:14












          You have to put nothing inside the constructor and you can keep the original one. This is constructor overload ing feature of java.
          – Priyadeep Datta
          Nov 23 '18 at 23:51




          You have to put nothing inside the constructor and you can keep the original one. This is constructor overload ing feature of java.
          – Priyadeep Datta
          Nov 23 '18 at 23:51


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440286%2fjava-cannot-find-a-specific-method-on-a-seperate-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu