Arraylist as Message when sending mail - java

I have this arraylist;
ArrayList<String> list = new ArrayList<String>();
I have populate this arraylist from some DB queries and i must send this list as e-mail.
public void sendMail(ArrayList carriers) throws Exception {
Email email = new SimpleEmail();
email.setHostName("mail.test.com.tr");
email.setSmtpPort(587);
email.setAuthentication("testuser#mail.test.com.tr","testuserpass");
email.setSSLOnConnect(false);
email.setFrom("testuser#mail.test.com.tr");
email.setSubject("Test Information List");
email.setMsg("Last 1 hour Information;\n"+carriers);
email.addTo("test#mail.test.com.tr");
email.send();
System.out.println("email sended succesfully.");
}
When i call this sendMail(list); method mail came to my mailbox succesfully. But all strings in this list showing side by side in message body normally. I want all strings align vertically.Let me explain;
Now;
trying1, trying2, trying3
Desired format;
trying1
trying2
trying3
How can i handle it?
--SOLVED--
StringBuilder b = new StringBuilder();
for(Object carrier : carriers)
b.append(carrier).append("\n");
String carriersString = b.toString();
Above lines added to sendMail() method, beginning of code. And below lines editing to;
email.setMsg("Last 1 hour Information;\n"+carriersString);
Thanks to #Icewind

You have to manually concatenate the strings to your desired format. The default toString() method will concatenate the values by a comma.
Something like this:
StringBuilder b = new StringBuilder();
for(String carrier : carriers)
b.append(carrier).append("\n");
String carriersString = b.toString();
or with StringUtils in apache commons (http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html):
String carriersString = StringUtils.join(carriers, "\n");
...snip...
email.setMsg("Last 1 hour Information;\n"+carriersString);

Related

Getting original text after using stanford NLP parser

Hello people of the internet,
We're having the following problem with the Stanford NLP API:
We have a String that we want to transform into a list of sentences.
First, we used String sentenceString = Sentence.listToString(sentence); but listToString does not return the original text because of the tokenization. Now we tried to use listToOriginalTextString in the following way:
private static List<String> getSentences(String text) {
Reader reader = new StringReader(text);
DocumentPreprocessor dp = new DocumentPreprocessor(reader);
List<String> sentenceList = new ArrayList<String>();
for (List<HasWord> sentence : dp) {
String sentenceString = Sentence.listToOriginalTextString(sentence);
sentenceList.add(sentenceString.toString());
}
return sentenceList;
}
This does not work. Apparently we have to set an attribute " invertible " to true but we don't know how to. How can we do this?
In general, how do you use listToOriginalTextString properly? What preparations do you need?
sincerely,
Khayet
If I understand correctly, you want to get the mapping of tokens to the original input text after tokenization. You can do it like this;
//split via PTBTokenizer (PTBLexer)
List<CoreLabel> tokens = PTBTokenizer.coreLabelFactory().getTokenizer(new StringReader(text)).tokenize();
//do the processing using stanford sentence splitter (WordToSentenceProcessor)
WordToSentenceProcessor processor = new WordToSentenceProcessor();
List<List<CoreLabel>> splitSentences = processor.process(tokens);
//for each sentence
for (List<CoreLabel> s : splitSentences) {
//for each word
for (CoreLabel token : s) {
//here you can get the token value and position like;
//token.value(), token.beginPosition(), token.endPosition()
}
}
String sentenceStr = sentence.get(CoreAnnotations.TextAnnotation.class)
It gives you original text. An example for JSONOutputter.java file :
l2.set("id", sentence.get(CoreAnnotations.SentenceIDAnnotation.class));
l2.set("index", sentence.get(CoreAnnotations.SentenceIndexAnnotation.class));
l2.set("sentenceOriginal",sentence.get(CoreAnnotations.TextAnnotation.class));
l2.set("line", sentence.get(CoreAnnotations.LineNumberAnnotation.class));

Vaadin access content of sql container

EDITED!
I retriev data from a mysql DB using Vaadin, SQLContainer and the Freeformquery. Now I want to get all Descriptions in one String (later each string will printed/added to a text file).
....
String text ="";
FreeformQuery subcatExtractionQuery = new FreeformQuery("select Description from customers", connectionPool);
SQLConateiner s = new SQLContainer(subcatExtractionQuery);
Collection<?> c = s.getContainerPropertyIds();
for(Object o : c){
Property<?> p = s.getContainerProperty(o, "Description");
text+=(String)p.getValue();
}
System.out.println(text);
I get the Error: java.lang.NullPointerException
Problem line is the text+=...if I remove it, no error appears. BUT The Query returns Data!
These descriptions are Strings. I need to have all words of each string in ony single list of tokens (i already have a method to create a token list of a file.)
(Don't ask if this makes sense, it's just an example).
How can I access the Strings of my sql container? Till now I only used the container als data source of a table and didn't access the single items/strings.
I need a for loop to get each String...how does this work?
Its getItemIds insted of getContainerPropertyIds. So this words
....
String text ="";
FreeformQuery subcatExtractionQuery = new FreeformQuery("select Description from customers", connectionPool);
SQLConateiner s = new SQLContainer(subcatExtractionQuery);
Collection<?> c = s.getItemIds();
for(Object o : c){
Property<?> p = s.getContainerProperty(o, "Description");
text+=(String)p.getValue();
}
System.out.println(text);

Splitting user pattern matcher

I have some entries in log and I want to use Pattern matcher to get the entries out of log.
Log entries
1223-12-23 00:00:00 exception : 1223. Operation Cannot be done
1223-12-24 00:00:01 exception : 1221. Operation Cannot be done
I want to get entries like
String [] date = {1223-12-23 00:00:00, 1223-12-24 00:00:01}
String [] message = {exception : 1223. Operation Cannot be done, exception : 1221. Operation Cannot be done}
Is there an efficient way to do this.
I already used Flat File Parsing Library to perform a similar task.
Better than my other answer:
// dynamic list of strings for dates and messages
List<String> dates = new ArrayList<>();
List<String> messages = new ArrayList<>();
// split your logfile by line
String[] lines = yourLogFileContentAsString.split("\n");
for (String line : lines) {
// dates are characters 0-19
dates.add(line.substring(0, 20));
// message starts at character 21
messages.add(line.substring(21);
}
// you wanted arrays
String[] datesArray = dates.toArray(new String[0]);
String[] messagesArray = messages.toArray(new String[0]);

update List of users in web services

I am trying to update a TextArea with a userList whenever a client enters as an admin. However the first client has only his name and the second has his name with the first client. What I want is to be able to update the list for the first client. For example, if a third client joined in, it should update the list for the first and second clients.
Here is my webservice method
public String getuserList ()
{
String usname = "";
synchronized(username)
{
for (int i = 0; i < username.size(); i++)
{
usname = usname + "\n" + username.get(i);
}
return usname;
}
}
any suggestions?
Thanks.
From an API design standpoint, I would expect a getUserList to return a List than a String. For all other purposes, your implementation looks just OK.
You may consider using StringBuilder to build the final string than the + operator.
EDIT : to show an example scenario.
StringBuilder sb = new StringBuilder();
for (String s : username)
{
sb.append(s);
sb.append("\n");
}
return sb.toString();

creating an object in java

i have a simple doubt in android programming. I am not familiar with java coding.so it might be a simple problem.
In the first two lines I am retrieving an array, which i passed from another activity to this activity...Then i am creating an array list . I am creating an object in the 4th line. Now comes the problem ...
I have to run a for loop to get the url value, which i have to pass it in the BaseFeedParser class. but i cant use the 4th line, i.e creating the object inside the loop because it will create a new object each time... which should not happen ... how can i fix this probelm?
Intent myintent = getIntent();
String[] ActiveURL = myintent.getStringArrayExtra("URL");
List<String> titles = new ArrayList<String>();
BaseFeedParser parser = new BaseFeedParser(url);
// fetching all active URLs
for (int i = 0; i < ActiveURL.length + 1; i++) {
url = ActiveURL[i];
messages.addAll(parser.parse());
}
// now getting the titles out of the messages for display
for (Message msg : messages) {
titles.add(msg.getTitle());
}
Thanks in advance ...
There are some problems in your java code :
Intent myintent = getIntent();
//variables are named in camel case, starting with a lower case letter
String[] activeURL = myintent.getStringArrayExtra("URL");
List<String> titles = new ArrayList<String>();
//we will use parser later, see below
//BaseFeedParser parser = new BaseFeedParser(url);
// fetching all active URLs
//it's very easy to loop through a table in java / C / C++
//learn the pattern, it's the simplest, you got confused with the final index
for (int i = 0; i < activeURL.length ; i++) {
//here you don't change the former object url was referencing,
//you are saying that you give the name url to another object in the array
//it doesn't create any new item, change giving them a name to use them
url = activeURL[i];
//create a new parser for each url, except if they can be recycled
//i.e they have a property setUrl
messages.addAll( new BaseFeedParser(url).parse());
}
// now getting the titles out of the messages for display
for (Message msg : messages) {
titles.add(msg.getTitle());
}
Indeed, you could even shorten the whole thing by
Intent myintent = getIntent();
String[] activeURL = myintent.getStringArrayExtra("URL");
List<String> titles = new ArrayList<String>();
// fetching all active URLs
//use a for each loop
for ( String url : activeURL ) {
//loop through messages parsed from feed to add titles
for (Message msg : new BaseFeedParser(url).parse() ) {
titles.add(msg.getTitle());
}
}
if you don't need the List of Message you called messages.

Categories