string comparison with null

F

focode

i have a form in which their are textFields when the user set his data
in that form and press next button i get all the data in form of
string , then i have to check if any field was left empty , if it is
so the application sends the user back to the form to fill the
required field , my problem is i have the value of the textField in
form of string , how will i know that this string contains nothing , i
used compareTo fuction i which the first parameter was string from the
textField and the second parameter i ahve taken an another string
which is null eg

String str = null;
int value = textFIeldString.compareTo(str);

but this throws exception

tell some other way to implement this ..

thanks and regards
Arunesh
 
L

Lew

To test for null use:

if (textFIeldString == null) { ... }

to test for zero length I tend to use:

if (textFIeldString.length() == 0) { ... }

To test for empty, combine those two:

( text == null || text.length() == 0 )
 
W

Wojtek

RedGrittyBrick wrote :
( text == null || text.trim().length() == 0 )

Next ...

I wrote a small method which left-justifies text. The idea being that
any text only has one space between words. This is useful in doing
searches because you know that all user input is correctly and
identically formatted. And it looks good on printouts.

-------------------------------------
text = Convert.leftJustify(text);

if ( text.length() == 0 )
-------------------------------------

And just because I am a nice guy (be gentle...):

-------------------------------------
/**
* This method formats a String. <br>
* <br>
* It places the first non-white space character at the left, and
removes all extra spaces. <br>
* So "&nbsp;a&nbsp;bc&nbsp;&nbsp;&nbsp;cd" will be returned as
"a&nbsp;bc&nbsp;cd"
*/
public static String leftJustify( String theValue )
{
char charArray[];

try
{
charArray = theValue.toCharArray();
}
catch (NullPointerException e)
{
return "";
}

StringBuffer out = new StringBuffer( charArray.length + 1 );

// remove any leading whitespace
boolean isSpace = true;

for (int c = 0; c < charArray.length; c++)
{
// leave CRLF for multiline inputs
if (!(charArray[c] == '\n' || charArray[c] == '\r') &&
Character.isWhitespace( charArray[c] ))
{
if (!isSpace)
out.append( ' ' );

isSpace = true;
}
else
{
out.append( charArray[c] );
isSpace = false;
}
}

// remove trailing space
if (isSpace && out.length() > 0)
{
String justified = out.toString();

return justified.substring( 0, justified.length() - 1 );
}

return out.toString();
}
-------------------------------------

Test:
System.out.println("'" +
ca.rcmp.esi.utility.tools.Convert.leftJustify(null) + "'");
System.out.println("'" +
ca.rcmp.esi.utility.tools.Convert.leftJustify("") + "'");
System.out.println("'" +
ca.rcmp.esi.utility.tools.Convert.leftJustify(" ") + "'");
System.out.println("'" +
ca.rcmp.esi.utility.tools.Convert.leftJustify(" b c e ") + "'");
System.out.println("'" +
ca.rcmp.esi.utility.tools.Convert.leftJustify("b c e") + "'");

Result:
''
''
''
'b c e'
'b c e'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top