简体   繁体   中英

I'm stuck trying to write a loop through an array for results in a toString

/**
     * get a formatted string with information about a competition.
     * 
     * @return String String with information about a competition.
     * 
     * The output should be in the following format:
     * <pre>
     * Rodent's Information:
     * Rat RFID 787878787
     * Gender: F
     * Vaccination status: false
     * 
     * Maze Information:
     * Start Time: 00:00:00
     * End Time: 01:00:05
     * Actual Time: 01:00:05
     * Contest Time: 00:59:30
     * </pre>
     * 
     */
    public String toString()
    {
        // your code here, replace the "X" and -9 with appropriate
        // references to instance variables or calls to methods
        String output = "Competition Description: " + this.desc
            + "\nCompetition Count: " + this.count + "\n";
        output += "Competition Results:" + "\n";
        // loop through the array from beginning to end of populated elements
        for (int i = 0; i < this.nextPos; ++i)
        {
            this.results[i].getRFID();
            this.results[i].getGender();


            // get toString() for each result


        return output;
    }

Hi everyone, I've been stuck on writing this toString for a couple of days now. Can someone please help me figure out how to write a loop for showing all the elements in the array from beginning to end. I just keep coming up stuck. As you can see, I have started writing a loop but now I have no clue if it is started out right. Thanks!

You haven't added what you're getting to the output String in your for() loop! You will need to change it to something like:

for (int i = 0; i < this.nextPos; ++i)
{
    output += this.results[i].getRFID();
    output += this.results[i].getGender();
    output += "\n";
}

Add any other formatting you like around this. The comments in your code indicate that you'll want to add a String like "Rodent's Information:" each time through the loop, as well as titles and indicators for each of the fields and line breaks in between them.

Good luck!

Also, to expand upon what @Matt said in the comments below your question, your comparison in your for() loop is very odd and likely isn't doing what you want it to (although maybe it is, and we're all just sticklers for convention). Normally, when looping through an array or collection, you will compare to the collection's length rather than something in the "next position" (which is what I assume the meaning of your variable is).

Hm, if you do it in a loop and you do it often, you might consider StringBuilder . String s are immutable in Java, you'll just get a bunch of new strings spawning everywhere in that loop, because of it. IYKWIM

A short example

StringBuilder output = new StringBuilder("");
for(int i = 0; i < this.nextPos; ++i) {
 output.append(this.results[i].getRFID());
 ...  
}

return output.toString();

If you want to concat the result, you just have to do similar to what you have done here output += "Competition Results:" + "\\n"; . Just do the same inside the loop:

 for (int i = 0; i < this.nextPos; ++i)
        {
            output += this.results[i].getRFID().toString();
            output += " "; // you may want to separate the strings
            output += this.results[i].getGender().toString();

        }

Btw this approach is very slow , see here comparisons about different string contact techniques.

An faster approach is using StringBuilder :

StringBuilder sb = new StringBuilder();

  for (int i = 0; i < this.nextPos; ++i)
            {
                sb.append(this.results[i].getRFID().toString());
                sb.append(this.results[i].getGender().toString());

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM