Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-16
18
Medium Priority
?
199 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

604 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