0
  • Problem: I am unable to return values entered in an Array to a preview textbox
  • Ideal solution: User presses refresh, the box is populated with the objects in the array.
  • JDK: 1.8

Working Command Line Version Code:

public static void printAll(Member[] platoon){
        //System.out.println("You are in the third loop");
        for (int cntr=0;cntr<platoon.length;cntr++){
            System.out.println("Member Name: " + platoon[cntr].getName() + " " + "Join Date: " + platoon[cntr].getJoinDate());
        }
    }

The above code takes a instantiated array of object type Members and returns via get methods each value stored in each Member(String Name, int day, int month, int year);.

I am attempting to do the same thing with the GUI abilities of Java. My code is somewhere around 300 lines, I am definitely willing to post it all as this is the last thing keeping me from finishing my project.

Below I write the action event that I want to use to set the text in the preview textfield with the current contents of the platoon Array. All I was attempting to do with this was place the print out from the above code into the box, it does not like the type VOID so i switched it to the return type String. Now It doesn't appear I can store the results of a For loop as a String? I am surley missing something vital.

GUI Code:

}else if(event.getSource() == refreshButton){
        displayText.setText(setPreview(platoon));

    }
public static String setPreview(Member[] platoon){
    //System.out.println("You are in the fourth loop");
    preview = (for (int cntr=0;cntr<platoon.length;cntr++){
        System.out.println("Member Name: " + platoon[cntr].getName() + " " + "Join Date: " + platoon[cntr].getJoinDate()););
        return preview;
    }
}

Thank you all for your help, will keep this OP updated to help future Stack Overflow members with this issue.

2
  • StringJoiner comes to mind; JTextArea#append or a JList if you're using Swing Commented May 2, 2015 at 22:06
  • Is this using Swing? For better help sooner, post an MCVE (Minimal Complete Verifiable Example) or SSCCE (Short, Self Contained, Correct Example). Commented May 3, 2015 at 5:25

3 Answers 3

1

Loops don't return results, interesting idea, Java just doesn't do.

You could...

Use a StringJoiner

StringJoiner joiner = new StringJoiner("\n");
for (int cntr=0;cntr<platoon.length;cntr++){
    joiner.add("Member Name: " + platoon[cntr].getName() + " " + "Join Date: " + platoon[cntr].getJoinDate());
}   
return joiner.toString();

You could...

Use JTextArea#append

    for (int cntr = 0; cntr < platoon.length; cntr++) {
        ta.append("Member Name: " + platoon[cntr].getName() + " " + "Join Date: " + platoon[cntr].getJoinDate() + "\n");
    }

You could...

Use JList which is designed to present a list of "stuff".

See How to use lists for more details

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

Comments

0

Your problem is in your setPreview method. You using System.out.println which is an instruction to print text to the system's standard output device (in most cases a terminal window). It does not return a string, and you cannot concatenate a string to a variable using this syntax:

preview = for(...) { ... }

Since setPreview will return a single String I suspect you want to do this instead:

public static String setPreview(Member[] platoon){
    String result = "";
    for (int cntr=0;cntr<platoon.length;cntr++){
        if (result.length() > 0) result += " ";   // put a space before the next output
        result += "Member Name: " + platoon[cntr].getName() + " " + "Join Date: " + platoon[cntr].getJoinDate());
    }
    return result;
}

This will build up the output string you need which can then be set to the text field or whatever is appropriate.

3 Comments

Especially in Java 8, there should never be any reason to do String concatenation within a loop (not that you should be doing it anywhere else either)
Doesn't your StringJoiner suggestion also use String concatenation throughout the building of the parameter? joiner.add("Member Name: " + platoon[cntr].getName() + " " + "Join Date: " + platoon[cntr].getJoinDate());
"Member Name: " + platoon[cntr].getName() + " " + "Join Date: " + platoon[cntr].getJoinDate()) will be optimised by the compiler (to use an internal StringBuilder), but your code is doing result += {StringBuilder}.toString() ... more or less. Having looked into how StringJoiner works (cause I was worried about this to), it uses a StringBuilder...so no String concaternation
0
  1. Override the toString() method in Member:

    @Override
    public String toString() {
        return "Member Name: " + getName() + " Join Date: " + getJoinDate();
    }
    
  2. PrintAll() will become (but you don't need it any more):

    public static void printAll(Member[] platoon) {
        for (int cntr = 0; cntr < platoon.length; cntr++)
            System.out.println(platoon[cntr].toString());
    }
    
  3. setPreview() will become (NOTE: this is only to show the principle, for better ways how to concatenate Strings in a loop see @MadProgrammer's answer):

    public static String getPreviewText(Member[] platoon) {
        String s = platoon.length > 0 ? platoon[0].toString() : "";
        for (int i = 1; i < platoon.length; i++)
             s += "\n" + platoon[i].toString();
        return s;
    }
    
    public static void setPreview(Member[] platoon) {
        displayText.setText(getPreviewText(platoon));
    }
    

4 Comments

While I'm, personally, not a fan of overriding the toString method to provide "display" output (I like to use for debugging), it's not a major issue, just need to beware that it has it's limitations, but I will complain about the String concaternation in a loop, especially in Java 8, there's simply no excuse to do this any more.
@MadProgrammer Okay, you could choose another method, but if I would write a "debug" (more of a state) output, then I would probably do it as he did. I am no Java 8 expert, I was just showing how to do and stay as close to his code as possible, it looks pretty easy to me, how would you improve it? Isn't it optimized by the compiler?
String concatenation in a loop isn't optimised by the compiler, it simply can't work it out. You could use a StringBuilder, but, Java 8 introduced the StringJoiner class, which is SO much simpler. As to the toString, that's just a personal gripe and doesn't make the solution any less valid, it just grinds on me ;)
@Darkn3ssF4lls you are welcome, just noticed I might have some errors with the static (compared to your code) I think everything should be static except toString() to match your code, corrected now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.