Solved

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

Posted on 2008-06-16
18
193 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now