g118481
asked on
How to extract everything left or right of a space from a variable passed from a form?
Experts,
I am passing a form field to my action page (JSP).
This form field has two names in it that are separated by a space.
EXAMPLE: JOHN DOE
The form is actioned to a JSP page for posting to a DB table.
What I need to do, is separate these two parts into separate strings.
How do I do that is JSP(JAVA)?
Thanks in advance for your time.
(Also, I hate saying this, but I need a solution ASAP.)
I am passing a form field to my action page (JSP).
This form field has two names in it that are separated by a space.
EXAMPLE: JOHN DOE
The form is actioned to a JSP page for posting to a DB table.
What I need to do, is separate these two parts into separate strings.
How do I do that is JSP(JAVA)?
Thanks in advance for your time.
(Also, I hate saying this, but I need a solution ASAP.)
You can split the string into array using spaces " " as delimiter.
String nameStr = "JOHN DOE" ;
String[] nameStrArr = nameStr.split(" ");
So if you call nameStrArr[0], the output is JOHN as nameStrArr[1] output is DOE.
Hope this help
String nameStr = "JOHN DOE" ;
String[] nameStrArr = nameStr.split(" ");
So if you call nameStrArr[0], the output is JOHN as nameStrArr[1] output is DOE.
Hope this help
ASKER
I am confused on how to write this.
Is this correct?
String nameStr = request.getParameter("MYFI ELD"); //field passed from form "JOHN DOE"
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String nameStrArr[0] SYS=nameStr.split(" "); //declare SYS value
String nameStrArr[1] SUBSYS=nameStr.split(" "); //declare SUBSYS value
Is this correct?
String nameStr = request.getParameter("MYFI
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String nameStrArr[0] SYS=nameStr.split(" "); //declare SYS value
String nameStrArr[1] SUBSYS=nameStr.split(" "); //declare SUBSYS value
Try this:
String nameStr = request.getParameter("MYFI ELD"); //field passed from form "JOHN DOE"
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String SYS = nameStrArr[0]; //declare SYS value - JOHN
String SUBSYS = nameStrArr[1]; //declare SUBSYS value - DOE
String nameStr = request.getParameter("MYFI
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String SYS = nameStrArr[0]; //declare SYS value - JOHN
String SUBSYS = nameStrArr[1]; //declare SUBSYS value - DOE
I think you should probably not make the assumption that there are two atoms only, so do
int numAtoms = atoms.length
int numAtoms = atoms.length
ASKER
I tested this, but am getting this error:
String nameStr = request.getParameter("MYFI ELD"); //field passed from form "JOHN DOE"
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String SYS = nameStrArr[0]; //declare SYS value - JOHN
String SUBSYS = nameStrArr[1]; //declare SUBSYS value - DOE
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// /
ERROR:
"Exception: java.lang.ArrayIndexOutOfB oundsExcep tion: 1 "
String nameStr = request.getParameter("MYFI
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String SYS = nameStrArr[0]; //declare SYS value - JOHN
String SUBSYS = nameStrArr[1]; //declare SUBSYS value - DOE
//////////////////////////
ERROR:
"Exception: java.lang.ArrayIndexOutOfB
ASKER
I ran this test, and got this result:
<%
String nameStr = request.getParameter("MYFI ELD"); //field passed from form "JOHN DOE"
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String SYS = nameStrArr[0]; //declare SYS value - JOHN
%>
RESULTS:
<%= nameStr %> = JOHN DOE
<%= SYS %> = JOHN DOE
It appears that this line is not splitting the name: String[] nameStrArr = nameStr.split(" ");
Where do I go from here?
<%
String nameStr = request.getParameter("MYFI
String[] nameStrArr = nameStr.split(" "); //splits the passed field
String SYS = nameStrArr[0]; //declare SYS value - JOHN
%>
RESULTS:
<%= nameStr %> = JOHN DOE
<%= SYS %> = JOHN DOE
It appears that this line is not splitting the name: String[] nameStrArr = nameStr.split(" ");
Where do I go from here?
>>"Exception: java.lang.ArrayIndexOutOfB oundsExcep tion: 1 "
As i said, don't assume the array has two elements. Also use the regex i posted
As i said, don't assume the array has two elements. Also use the regex i posted
ASKER
Chej,
I appreciate your candor.
Can you PLEASE give me a code example to solve this issue?
I tried this, but it doesn't work.
It fails on SYS.
<%
String nameStr = request.getParameter("MYFI ELD"); //field passed from form "JOHN DOE"
String[] nameStrArr = nameStr.split(" "); //splits the passed field
for (int i=0;i<nameStrArr.length;i+ +)
String SYS = nameStrArr[0]; //declare SYS value - JOHN
%>
<%= SYS %>
I appreciate your candor.
Can you PLEASE give me a code example to solve this issue?
I tried this, but it doesn't work.
It fails on SYS.
<%
String nameStr = request.getParameter("MYFI
String[] nameStrArr = nameStr.split(" "); //splits the passed field
for (int i=0;i<nameStrArr.length;i+
String SYS = nameStrArr[0]; //declare SYS value - JOHN
%>
<%= SYS %>
Try something like the following:
<%
String nameStr = request.getParameter("MYFIELD");
String[] nameStrArr = nameStr.split("\\s*"); //splits the passed field
switch (nameStrArr.length) {
case 1:
// Single name
String name = nameStrArr[0];
break;
case 2:
// Two parts to name
String firstName = nameStrArr[0];
String lastName = nameStrArr[1];
break;
case 3:
// Three parts to name - perhaps middle name given - discard it
String firstName = nameStrArr[0];
String lastName = nameStrArr[2];
break;
default:
// Error condition - handle it
}
%>
ASKER
Cehj,
I have ran your suggestion code for a two part string, but get an error when trying to output the value of the firstName and lastName strings to check their values.
<%
String nameStr = request.getParameter("MYFI ELD");
String[] nameStrArr = nameStr.split("\\s*"); //splits the passed field
switch (nameStrArr.length) {
case 1:
// Two parts to name
String firstName = nameStrArr[0];
String lastName = nameStrArr[1];
break;
default:
// Error condition - handle it
}
%>
<%= firstName %> <%= lastName %>
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// //////////
HER IS THE ERROR:
"cannot resolve symbolsymbol : variable firstName location: class com.ibm._jsp._sys_5F_subsy s_5F_test out.print( firstName );
^"
"cannot resolve symbolsymbol : variable lastName location: class com.ibm._jsp._sys_5F_subsy s_5F_test out.print( lastName );
^"
I have ran your suggestion code for a two part string, but get an error when trying to output the value of the firstName and lastName strings to check their values.
<%
String nameStr = request.getParameter("MYFI
String[] nameStrArr = nameStr.split("\\s*"); //splits the passed field
switch (nameStrArr.length) {
case 1:
// Two parts to name
String firstName = nameStrArr[0];
String lastName = nameStrArr[1];
break;
default:
// Error condition - handle it
}
%>
<%= firstName %> <%= lastName %>
//////////////////////////
HER IS THE ERROR:
"cannot resolve symbolsymbol : variable firstName location: class com.ibm._jsp._sys_5F_subsy
^"
"cannot resolve symbolsymbol : variable lastName location: class com.ibm._jsp._sys_5F_subsy
^"
ASKER
Increasing points.
>>I have ran your suggestion code for a two part string
That's not what i gave you for a two part String. It should be case 2
If you want to print the values, you must do so within the block in which they're declared, not outside it
That's not what i gave you for a two part String. It should be case 2
If you want to print the values, you must do so within the block in which they're declared, not outside it
ASKER
This is so frustrating.
I have your code example in place.
However, it does not return anything when I try to output the results.
I am expecting to output firstName and lastName. but nothing.
Can you test from your end and pass it a form field value?
Sorry for being such a burden.
I have your code example in place.
However, it does not return anything when I try to output the results.
I am expecting to output firstName and lastName. but nothing.
Can you test from your end and pass it a form field value?
Sorry for being such a burden.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Super!
This now works fantastic.
Thank you very much for your time and efforts!
This now works fantastic.
Thank you very much for your time and efforts!
ASKER
Thank you!
:-)
Open in new window