Home > Mobile >  Editing java email services
Editing java email services

Time:04-24

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 "{,},(,),;,::,".

CodePudding user response:

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.

  •  Tags:  
  • java
  • Related