1

I have a form that the user fills out. When the user has completed the form and submits the form it then creates a .txt file that is stored and also sends an email. I was able to have the .txt form print certain strings as follows.

PrintWriter pw = new PrintWriter(fw);
                        
pw.write("DATE: " + datea + "\n" + "SHIFT: " + shiftboxa + "\n" + "SHIFT LEADER: " + shiftleaderboxa + "\n\n" + "DUTY CREWS:" );

if(b2003.equals(true)) {
pw.write("\n\n" + "2003: \n" + crew20031a + " " + time20031a+ "\n" + crew20032a + " " + time20032a);
                        }

if(b2040.equals(true)) {
pw.write("\n\n" + "2040:" + "\n" + crew20401a + " " + time20401a  + "\n" + crew20402a + " " + time20402a +  "\n" + crew20403a + " " + time20403a +  "\n" + crew20404a + " " + time20404a );
                        }

if(b2041.equals(true)) {
pw.write("\n\n" + "2041:" + "\n"+ crew20411a + " " + time20411a + "\n" + crew20412a + " " + time20412a + "\n" + crew20413a + " "+ time20413a + "\n" + crew20414a + " " + time20414a);
                        }

if(b2042.equals(true)) {
pw.write("\n\n" + "2042:" + "\n" + crew20421a + " " + time20421a + "\n" + crew20422a + " "  + time20422a + "\n"+ crew20423a + " " + time20423a + "\n" + crew20424a + " " + time20424a ); 
                        }

//b2003, b2040, b2041, b2042 are all Booleans that change based on actions taken in the GUI.

//also all of the non text objects are just Strings.

When it comes to the email portion is where the problem arises. This is how I currently have it set up:

message.setText("Please DO NOT reply to this E-Mail." + "\n" +
"Please review at your earliest convenience."+
                            
"2003: \n" + crew20031a + " " +time20031a + "\n" + crew20032a + " " + time20032a+ "\n\n\n" + 
                        
"2040:" + "\n" + crew20401a + " " + time20401a + "\n" + crew20402a + " "+ time20402a +  "\n" + crew20403a + " " + time20403a + "\n" + crew20404a + " " + time20404a +"\n\n" + 
                            
"2041:" + "\n"+ crew20411a + " " + time20411a + "\n" + crew20412a + " " + time20412a + "\n" + crew20413a + " "+ time20413a + "\n" + crew20414a+ " " + time20414a + "\n\n" + 
                        
"2042:" + "\n" + crew20421a + " " + time20421a +"\n" + crew20422a + " "  + time20422a + "\n"+ crew20423a + " " + time20423a + "\n" + crew20424a + " " + time20424a + "\n\n");

I am trying to have it set up similar to the .txt file where if a boolean is equal to true then it will print out that line and if not then the line will be omitted.

i.e.:

if(b2040.equals(true)){message.setText("2040:" + "\n" + crew20401a + " " + time20401a + "\n" + crew20402a + " "+ time20402a +  "\n" + crew20403a + " " + time20403a + "\n" + crew20404a + " " + time20404a +"\n\n");}

My issue is every time I try to do an additional .setText it will clear out the previous .setText. Furthermore if I try and just insert and "if statement" into the .setText it will throw all sorts of errors such as needing to insert "{,},(,),;,::,".

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Luki
  • 49
  • 6
  • 2
    Use a StringBuilder, that is perfect for building larger Strings. In the end you can do `message.setText(sb.toString());` – JayC667 Apr 23 '22 at 17:15

1 Answers1

2

Like @JayC667 already wrote very briefly:

You should use a StringBuilder to create one final string that you then write to the file and send as email:

StringBuilder message = new StringBuilder();
message.append("DATE: ").append(datea).append("\nSHIFT: ").append(shiftboxa);
message.append("\nSHIFT LEADER: ").append(shiftleaderboxa).append("\n\nDUTY CREWS:");

if (Boolean.TRUE.equals(b2003)) {  // compare Booleans in a null-safe way
  message.append("\n\n2003: \n").append(crew20031a).append(' ').append(time20031a);
  message.append('\n').append(crew20032a).append(' ').append(time20032a);
}

// continue with your other conditional appends here

// supposing that fw is a FileWriter - but you have to catch IOException then!
fw.write(message.toString());

// mail sending then with this:

message.setText(message.toString());

You can split the appends over multiple lines for better readability, just as you prefer, or have them all in one line.

cyberbrain
  • 3,433
  • 1
  • 12
  • 22
  • This method worked! Unrelated question though, why is it bad practice to compare Booleans with .equals? – Luki Apr 23 '22 at 17:51
  • 1
    Because the condition in an if always evaluates only a `boolean` (primitive), and a `Boolean` will be auto-unboxed to that. So it clutters your code. See this answer: https://stackoverflow.com/a/2661121/2846138 – cyberbrain Apr 23 '22 at 17:57
  • 2
    About the Boolean comparison: actually, it sometimes makes sense to compare a Boolean to a literal with `equals`. For example, `if(Boolean.TRUE.equals(booleanVar))` will always work and branch only if the booleanVar contains a Boolean.TRUE. On the other side a simple `if(booleanVar)` will throw a NullPointerException if booleanVar is null. – Alex Sveshnikov Apr 23 '22 at 18:16
  • @AlexSveshnikov you are right of course. How could I forget about that? But still the way it was done in the question did not serve that purpose. – cyberbrain Apr 23 '22 at 18:28
  • 2
    I just updated my answer to include null-safe conditions for `Boolean` value of `b2003`. – cyberbrain Apr 23 '22 at 18:32