0

I am just trying to call a simple method that returns a string and for some reason, I am blanking and cannot remember for the life of me how to properly do this. I am trying to calling the method getInfo() in the Bands class and to print out the string bandInfo thats inside it:

public class App {


public static void main(String[] args) {

    Bands[] bands = new Bands[5];

    bands[0] = new Bands("Joe", "Rick", "Nick", "Dalton", "Doylestown, PA", "RockOn", 4000.50 , "Rock");
    bands[1] = new Bands("Luke", "Bill", "Ian", "Matt", "State College, PA", "Blink182", 3500.50 , "Alternative");
    bands[2] = new Bands("Corey", "Noah", "Jon", "Kaleb", "Philadelphia, PA", "Rise Against", 10000.50 , "Rock");
    bands[3] = new Bands("Jake", "Joey", "Mike", "Mac", "New York, NY", "Thousand Foot Krutch", 2000.50 , "Rock");
    bands[4] = new Bands("Bob", "Jeff", "Dom", "Mark", "King of Prussia, PA", "Skillet", 5500.50 , "Rock");

    bands[0].compete();
    bands[1].compete();
    bands[2].compete();
    bands[3].compete();
    bands[4].compete();

    for (int i = 0; i < 5; i++) {
        String bandInfo = getInfo(bandInfo);
    }
}
}

And here is my Bands class code:

import java.util.Random;

public class Bands {

private String singerName;
private String guitarName;
private String bassistName;
private String drummerName;
private String Hometown;
private String bandName;
private double income;
private String genre;
private int score;

public Bands(String singerName, String guitarName, String bassistName, String drummerName, String Hometown, String bandName, double income, String genre)
{
    this.singerName = singerName;
    this.guitarName = guitarName;
    this.bassistName = bassistName;
    this.drummerName = drummerName;
    this.bandName = bandName;
    this.Hometown = Hometown;
    this.income = income;
    this.genre = genre;
    this.score = -1;
}

public void compete()
{     
    Random rand = new Random();
    this.score = rand.nextInt(20);

}

public String getInfo()
{
    String bandInfo = "Band: " + this.bandName + ", Singer: " + this.singerName + ", Guitarist: " + this.guitarName + ", Bassist: " + this.bassistName + 
                      ", Drummer: " + this.drummerName + ", Hometown: " + this.Hometown + ", Income: " + this.income + ", Genre: " + 
                      this.genre + ", Final Score: " + this.score;

    return bandInfo;
}
}
3
  • 1
    You call it the exact same way you called the compete method. Commented Jan 30, 2017 at 19:58
  • This isn't really relevant to your question (which is basically just bands[i].getInfo()), but generally if you're going to have a canonical "summary string" representing an instance of your class, the method should be called toString(). Then you can print an instance of your class directly (rather than calling a method and printing the resulting string). Commented Jan 30, 2017 at 20:09
  • Okay, thanks for the advice, Ill look into it! Commented Jan 30, 2017 at 20:09

1 Answer 1

2
for (int i = 0; i < 5; i++) {
    String bandInfo = bands[i].getInfo();
}

getInfo() is a instance method of Bands class. So you have to use an instance of Bands to invoke the method.

bands[i] refers a Bands object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that! For some reason, I was blanking, but thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.