DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page String and Arry Problem
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Mar 2008
Default String and Arry Problem - 03-22-2008, 04:52 AM

I am new very new to Java. I am trying to get the data from form and to email.

I am getting 3 fields of form two of them are String and one is array and want to store in one variable as to mail or print.

msg1 is String
msg2 is Array
msg3 is String

Now am trying to store like:

mailmsg=msg1+msg2+msg3;

System.out.println(mailmsg) this gt me the output msg1,last index of msg2 and msg3. I have problem with msg2 as this is array. How can I merge with
mailmsg=msg1+msg2+msg3;

Its Ok when there is a string but when array cames it became very difficuit for me to merge with string and mail it. Please check my following code

Thanks & best regards

Code:
String phone=req.getParameter("phone2");
String country=req.getParameter("country");
String[] btype=req.getParameterValues("btype");
 
msg1="<table border=0 cellpadding=4 align=center cellspacing=5 width=500>"+
     "<tr><td class=khan><font color=red>Country</font></td><td class=khan>"+country+"</td></tr>"+
     "<tr bgcolor=#EEEEEE><td class=khan><font color=red>Phone</font></td><td class=khan>"+phone+"</td></tr>";
 
	msg2="<tr><td class=khan><font color=red>Business Type</font></td><td class=khan>";
 
		for(i=0;i<btype.length;i++){
			if(!btype[i].e[]quals("")){
			btype[i]
			}
		}
 
	msg3="</td></tr>";
 
mailmsg=msg1+msg2+msg3;
System.out.println(mailmsg);
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Apr 2008
Default 04-01-2008, 06:43 AM

if msg2 is an array then you should store and retreive data on/from a particular index. you can do it like msg2[0] = "whatever written stores at index 0"; and so on.
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Mar 2008
Default 04-14-2008, 02:54 AM

Quote:
Originally Posted by farakhkhan@yahoo.com View Post
Code:
mailmsg=msg1+msg2+msg3;
System.out.println(mailmsg);
If you are using Java 1.5 or later, there is a convenient toString(Object[] a) method on java.util.Arrays.

So you could write it like this:
Code:
mailmsg=msg1+Arrays.toString(msg2)+msg3;
Otherwise, you'll just have to iterate:

Code:
mailmsg = msg1;
for(int i = 0; i < msg2.length; ++i) {
    mailmsg += msg2[i];
}
mailmsg += msg3;
If you're going to be doing this a lot and msg2 is going to be a large array, you'll want to build up the string in a StringBuffer (StringBuilder in Java 1.5 or later):

Code:
StringBuffer buf = new StringBuffer(msg1);
for(int i = 0; i < msg2.length; ++i) {
    buf.append(msg2[i]);
}
buf.append(msg3);
mailmsg = buf.toString();
Have fun!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection String For Microsoft SQLServer jo90way Microsoft SQL Server 0 01-12-2009 06:14 AM
String to formatted date sahuabinash Java 1 04-29-2008 11:03 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com