0

I am trying to run a check to see if a string is just white space. I tried using trim like this:

if( dataIn[ i ].trim() == "" )
    {
        throw new MailRecordException( "Line: " + lineNumber + ", Field: "
            + fieldNumber + " :Blank Field" );
    }

But it never registers as true. How could I run a check to see if a string is only whitespace?

Sc0tty
  • 49
  • 3
  • 8
  • Per the duplicate question, you need to use `Object.equals(Object)` to compare `String`(s). But, for your **specific** case I would suggest you prefer [`String.isEmpty()`](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#isEmpty--), like `data[i].trim().isEmpty()`. – Elliott Frisch Nov 18 '15 at 03:40

4 Answers4

0

You should use

dataIn[ i ].trim().equals("")

instead of

dataIn[ i ].trim() == ""

== tests whether the two operands refer to the same object.

equals() (which can be overridden) compares the values of the two objects.

Brian
  • 12,145
  • 20
  • 90
  • 153
0

Use equals instead of == for String comparison

dataIn[ i ].trim().equals("")
Nyavro
  • 8,806
  • 2
  • 26
  • 33
0

how about this?

if( dataIn[ i ].trim().length() == 0 ) //check for empty string
{
    throw new MailRecordException( "Line: " + lineNumber + ", Field: "
        + fieldNumber + " :Blank Field" );
}
William Ku
  • 798
  • 5
  • 17
0

dataIn[ i ].trim().length == 0

or

dataIn[ i ].trim().equals("")

NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
Rookie007
  • 1,229
  • 2
  • 18
  • 50