Home > Software engineering >  How to call toString() Method from one class to another class without main method in java
How to call toString() Method from one class to another class without main method in java

Time:06-18

I'm trying to make simple pokemon game.

How to I call toString method from Trainer.java to Arena.java? I tried to use this.trainer1.toString(), but it doesn't work. Also Arena.java has no main method.

while compiling programme, this error appears

Arena.java:14: error: illegal start of type
    this.trainer1.toString();
    ^
Arena.java:14: error: ';' expected
    this.trainer1.toString();
        ^
Arena.java:14: error: <identifier> expected
    this.trainer1.toString();
                          ^
Arena.java:15: error: illegal start of type
    this.trainer2.toString();
    ^
Arena.java:15: error: ';' expected
    this.trainer2.toString();
        ^
Arena.java:15: error: <identifier> expected
    this.trainer2.toString();
                          ^
6 errors

Here are the files

Trainer.java

import java.util.Random;

public class Trainer {

    private String nameTrainer;
    private Taschenmonster[] inventar = new Taschenmonster[6];

    // getter and setter Methods for attributes

    /*
     * getter and setter for nameTrainer
     */
    public String getNameTrainer() {
        return this.nameTrainer;
    }
    public void setNameTrainer(String nameTrainer) {
        this.nameTrainer = nameTrainer;
    }

    /*
     * getter and setter for inventar
     */
    // public Taschenmonster getInventar() {
    //     return this.inventar;
    // }
    // public void setName(String nameTrainer) {
    //     this.nameTrainer = nameTrainer;
    // }

    // constructor of class Trainer
    Trainer (String nameTrainer) {
        this.nameTrainer = nameTrainer;
    }

    /*
     * Adds Taschenmonster to Inventar of Trainer.
     */
    public void receive(int pos, Taschenmonster monsterObject) {
       if (pos < 6) {
           inventar[pos] = monsterObject;
       } 
    }

    /*
     * give out a randomly selected Taschenmonster for battle
     */
    public Taschenmonster send() {

        int random = new Random().nextInt(inventar.length);

        return inventar[random];

    }

    /*
     * Overriding the string method 
     */
    @Override
    public String toString() {
        return "Taschenmonster von "   nameTrainer   "\n"   inventar[0].getNameMonster()   " "   inventar[1].getNameMonster()   " "   inventar[2].getNameMonster()   " "   inventar[3].getNameMonster()   " "   inventar[4].getNameMonster()   " "   inventar[5].getNameMonster();    
    }

    
}

Arena.java

public class Arena {


    private Trainer trainer1;
    private Trainer trainer2;

    // constructor of class Arena
    Arena (Trainer trainer1, Trainer trainer2) {
        this.trainer1 = trainer1;
        this.trainer2 = trainer2;
    }

    // giving out the profile of the trainers
    // this.trainer1.toString();
    // this.trainer2.toString();


}

Main.java

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

        // making objects from class Taschenmonster
        Taschenmonster t1 = new Taschenmonster("Pikachu", 5, 7);
        Taschenmonster t2 = new Taschenmonster("Eevee", 5, 6);
        Taschenmonster t3 = new Taschenmonster("Charmander", 5, 4);
        Taschenmonster t4 = new Taschenmonster("Bulbasaur", 5, 5);
        Taschenmonster t5 = new Taschenmonster("Squirtle", 5, 2);
        Taschenmonster t6 = new Taschenmonster("Mewoth", 5, 3);

        // instantiating trainers from class Trainer
        Trainer trainer1 = new Trainer("Paul");
        Trainer trainer2 = new Trainer("Chris");

        // adding Taschenmonster to both trainers
        trainer1.receive(0, t1);
        trainer1.receive(1, t2);
        trainer1.receive(2, t3);
        trainer1.receive(3, t4);
        trainer1.receive(4, t5);
        trainer1.receive(5, t6);

        trainer2.receive(0, t1);
        trainer2.receive(1, t2);
        trainer2.receive(2, t3);
        trainer2.receive(3, t4);
        trainer2.receive(4, t5);
        trainer2.receive(5, t6);

        // giving out profiles of Players
        System.out.println(trainer1);
        System.out.println(trainer2);


    }
}

Taschenmonster.java

import java.util.*;

public class Taschenmonster {


    // class attributes
    private String nameMonster;
    private int lives, damageValue;

    // getter and setter Methods for attributes

    /*
     * getter and setter for nameMonster
     */
    public String getNameMonster() {
        return this.nameMonster;
    }
    public void setNameMonster(String nameMonster) {
        this.nameMonster = nameMonster;
    }

    /*
     * getter and setter for lives
     */
    public int getLives() {
        return this.lives;
    }
    public void setLives(int lives) {
        this.lives = lives;
    }

    /*
     * getter and setter for damageValue
     */
    public int getDamageValue() {
        return this.damageValue;
    }
    public void setDamageValue(int damageValue) {
        this.damageValue = damageValue;
    }


    // constructor of class Taschenmonster
    Taschenmonster (String nameMonster, int lives, int damageValue) {
        this.nameMonster = nameMonster;
        this.lives = lives;
        this.damageValue = damageValue;
    }


    /*
     * how much lives is remaining with Taschenmonster
     */
    public void receiveDamage() {
        this.lives--;
        if (this.lives == 0) {
            System.out.println("Caution! No lives left.");
        }
    }

    /*
     * gives out estimated attackValue of Taschenmonster in form of double. 
     */
    public int attackValue() {
        Random random = new Random();
        return this.damageValue *= random.nextInt() % 2;
    }

    /*
     * Overriding toString() Method and values of object 
     */
    @Override
    public String toString() {
        return nameMonster   " "   lives   " "   damageValue;
    }


}

CodePudding user response:

Issue

The compiler errors shown complain about method-invocations that are loosely dangling in your class. These method-invocations in a class must be located inside executable blocks like static { } or public String toString() { }.

Inside a block means inside curly braces like:

void method {
  // inside a block
  callOtherMethod();
}

Print fields of a class

To print out the trainers (fields of class Arena) you can add an instance method to the class. For example public void printTrainers() {}.

Therein add a printing call like System.out.println(). As argument to this method-invocation pass the trainer1 field or explicitly the expression trainer1.toString(), even longer this.trainer1.toString().

Then you can call the method (it should be public) from outside (e.g. in your main) like: arena.printTrainers();.

Code Example:

public class Arena {

    private Trainer trainer1;
    private Trainer trainer2;

    // constructor of class Arena
    Arena (Trainer trainer1, Trainer trainer2) {
        this.trainer1 = trainer1;
        this.trainer2 = trainer2;
    }

    public void printTrainers() {
        // giving out the profile of the trainers

        System.out.println(this.trainer1.toString());

        // this.trainer2.toString();
        System.out.println(trainer2); // equal result: calls toString implicitly
    }

}

Further spots

You can also print trainers from your main-method directly. Therefor they must be accessible like variables.

✔️ It will work like seen in your code:

// previous class lines omitted

    public static void main(String args[]) {  // executable-BLOCK begins

        // instantiating trainers from class Trainer
        Trainer trainer1 = new Trainer("Paul");  // declaration can also be outside the method- or executable-block
        Trainer trainer2 = new Trainer("Chris");

        // giving out profiles of Players
        System.out.println(trainer1);        // INSIDE the executable-block: valid invocation OK 
        System.out.println(trainer2);

    }  // executable-BLOCK ends

// further class lines omitted
}

⚠️ Warning: the heading comment states Players but you are printing Trainers.

  •  Tags:  
  • java
  • Related