Solved

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

Posted on 2008-06-16
18
196 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Title # Comments Views Activity
tomcat administrtor 12 69
check java version using powershell 13 307
collection output issue 9 69
Java array sort 10 36
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

697 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