2017-12-11 7 views
0

私のコード(Javaにあります)では、メールの件名に特定の文字列の出現が計算されています。その情報を表形式で表示するメールを送信することになっています。テーブルには、その文字列の "文字列名"と "カウント値"の2つのフィールドが含まれています。私はそれを行うことができますが、私は文字列のカウント値が0に等しい文字列のカウント値を表示したくありません。カウント変数は、その文字列が発生したときに1だけインクリメントします(count ++)。メールを送信するときに、htmlテーブルにカウント値(0に等しい)を表示しないようにします。

私はテーブル(html形式)で必要なときに番号と空文字列の両方を表示できるカウント変数に対してObjectコンベンションを選択することを考えていましたが、その増分をObject変数で実行することはできません。

この問題を解決する方法はありますか?以下は

コードスニペットです:

public static void main(String[] args) throws MessagingException{ 
    String host = "smtp.gmail.com", senderid = "<sender_id>@gmail.com", recipient = "<recipient_id>@gmail.com"; 
    String password = "*******"; 
    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", "587"); 

    // Get the Session object. 
    Session session = Session.getInstance(props, 
    new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(senderid, password); 
     } 
    }); 

    System.out.println("Sending mail"); 

    Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(senderid)); 
    message.setRecipients(Message.RecipientType.TO, 
    InternetAddress.parse(recipient)); 
    message.setSubject("Test"); 

    message.setContent(<table_format stored a string which accesses the count variable in its <td> tag>, "text/html"); 
    Transport.send(message); 

    System.out.println("Done"); 

} 

これは動作するコードスニペットです。カウント値は別のクラスから来ています。私はちょうど表の0であるカウント値の表示をスキップしたいと思う。

ありがとうございました。

+0

ゼロカウントを削除する場所のようなサウンドは、ここで結果文字列を解析する代わりに、ゼロカウントを返すコードにあります。あなたはそのコードを共有できますか? – Mureinik

+0

@Mureinikがコードスニペットを追加しました。 – Nikhil

答えて

0
public class Table { 

/* The count values are coming from static variables from the other class (Mail) where the increment is happening by checking the mail subject */ 
/* Below is where I am accesing the variables in the function*/ 

    public static String report_table = 
     "<table border='1' width='800' style='border-collapse:collapse;text-align:center'>" 
     + "<tr>" 
     + "<th style='padding:5px ; background-color:#78909c'>String</th>" 
     + "<th style='padding:5px; background-color: #78909c'>Count Value</th>" 
     + "</tr>" 

     + "<tr>" 
     + "<td style='background-color:#78909c;'>Filesize</td>" 
     + "<td>"+Mail.filesize_count+"</td>" 
     + "</tr>" 

     + "<tr>" 
     + "<td style='background-color:#78909c;'>Memory</td>" 
     + "<td>"+Mail.memory_count+"</td>" 
     + "</tr>" 
     + "<tr>" 
     + "<td style='background-color:#78909c;'>Content</td>" 
     + "<td>"+Mail.content_count+"</td>" 
     + "</tr>" 
     + "</table>" 

     +"<br><br>" 
     +"<b>" 
     +"Regards"+","+"<br>" 
     +"Nikhil" 
     +"</b>" ; 
} 

これは、カウント値がテーブルに格納されるクラスです。

+1

StringBuilderでStringを構築し、条件付きでスニペットにカウントを追加します。 –

+0

こんにちは@BillShannonは本当に助けになりました。ちょっと退屈な仕事でしたが、手動で条件をチェックして追加しましたが、現在は機能しています。これを指摘してくれてありがとう。 – Nikhil

関連する問題