Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Luke Strama
Luke Strama
6,928 Points

Problem with 'for' loop output (personal project)

Basic idea of the project:

As an investment broker, I input transactions for clients several times a day and it can be quite time consuming. I created a program that looks up the fund number and types out the full name for each fund in a format that I can then save to my work records for future reference.

There's 3 columns: Amount, Fund from and Fund to. If there is no amount typed in the 'amount' column, the program will skip over it and not look up the funds. Otherwise, it'll populate the information from a separate class that contains the fund information corresponding to its fund # code.

 JTextArea textarea = new JTextArea("Processed the following transactions for " 
                                                            + clientFirstName.getText() + "\'s " + account.getText().toUpperCase() + 
                                                            " as discussed via " + contactMethod.getText().toLowerCase() + " on " + 
                                                            date.getText() + " at " + time.getText() + ".\n\n" + textOutput()                                                                                                                       
                                                                                                                                    );
                                            JOptionPane.showMessageDialog(null, textarea, "Output", JOptionPane.PLAIN_MESSAGE);
public String textOutput() {
          String line = "";
          String addedLine;
          int count = 0;
          for (String amount : amountsInput) {
                    if ("".equals(amount)) {
                        //  skip over it
                    }
                    else {
                        addedLine = "- Switch $" + amountsInput.get(count) + " from " + fromFundsResult.get(count) + " to " + toFundsResult.get(count) + " W/O #\n";
                        count++;
                        line += addedLine;
                    }    
           }
           return line;
        }

So the program runs great the first time around but when I close the popup that displays the results and I try to input new information without killing the program first, everything corresponding to the "textOutput()" method duplicates the original inputs.

I'm confused where it's storing the original value when the 'line' variable is seemingly being reset to a blank string when textOutput() is being executed again... Also, the string that is created in the 'textarea' updates everything else correctly when I make a change.

Thoughts?? Thanks!