Solved

How to extract everything left or right of a space from a variable passed from a form?

Posted on 2008-06-16
18
195 Views
Last Modified: 2010-04-21
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.)
0
Comment
Question by:g118481
  • 9
  • 7
  • 2
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21793545
you can do
String[] atoms = field.split("\\s*");

Open in new window

0
 
LVL 6

Expert Comment

by:psychic_zero
ID: 21793604
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
0
 
LVL 1

Author Comment

by:g118481
ID: 21793849
I am confused on how to write this.
Is this correct?

String nameStr = request.getParameter("MYFIELD");   //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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:psychic_zero
ID: 21794068
Try this:

String nameStr = request.getParameter("MYFIELD");   //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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21794183
I think you should probably not make the assumption that there are two atoms only, so do

int numAtoms = atoms.length
0
 
LVL 1

Author Comment

by:g118481
ID: 21795556
I tested this, but am getting this error:
String nameStr = request.getParameter("MYFIELD");   //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.ArrayIndexOutOfBoundsException: 1 "

0
 
LVL 1

Author Comment

by:g118481
ID: 21795735
I ran this test, and got this result:

<%
String nameStr = request.getParameter("MYFIELD");   //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?



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21795851
>>"Exception: java.lang.ArrayIndexOutOfBoundsException: 1 "

As i said, don't assume the array has two elements. Also use the regex i posted
0
 
LVL 1

Author Comment

by:g118481
ID: 21795973
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("MYFIELD");   //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 %>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21796150
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
 
		
}
%>

Open in new window

0
 
LVL 1

Author Comment

by:g118481
ID: 21796326
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("MYFIELD");
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 %>&nbsp;<%= lastName %>

////////////////////////////////////////////////////////////////////////////////////////////////

HER IS THE ERROR:

"cannot resolve symbolsymbol  : variable firstName location: class com.ibm._jsp._sys_5F_subsys_5F_test      out.print( firstName );                
                                                                                   ^"

"cannot resolve symbolsymbol  : variable lastName location: class com.ibm._jsp._sys_5F_subsys_5F_test      out.print( lastName );                
                                                                                   ^"
0
 
LVL 1

Author Comment

by:g118481
ID: 21796377
Increasing points.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21796410
>>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
0
 
LVL 1

Author Comment

by:g118481
ID: 21797176
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.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 21797589
I noticed an error. Try this (i tested it)


<%
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
                firstName = nameStrArr[0];
                lastName = nameStrArr[2];
                break;
        default:
                // Error condition - handle it
 
                
}
%>

Open in new window

0
 
LVL 1

Author Comment

by:g118481
ID: 21797912
Super!
This now works fantastic.

Thank you very much for your time and efforts!
0
 
LVL 1

Author Closing Comment

by:g118481
ID: 31467594
Thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21798535
:-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question