Solved

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

Posted on 2008-06-16
18
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

688 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