Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java isEmpty() undefined for String?

Tags:

I'm working on a school project in eclipse, and when I try and use the isEmpty() method on a String, Eclipse shows up the following error:

The method isEmpty() is undefined for the type String

I've run the Java updates, but am still getting it. Is any reason why this method would be undefined?

like image 411
Aaron Moodie Avatar asked Sep 27 '09 12:09

Aaron Moodie


People also ask

Does isEmpty work on string?

isEmpty() String method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false. The isEmpty() method of String class is included in java string since JDK 1.6. In other words, you can say that this method returns true if the length of the string is 0.

What does isEmpty () do in Java?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

What is the syntax for isEmpty?

This example uses the IsEmpty function to determine whether a variable has been initialized. MyCheck = IsEmpty(MyVar) ' Returns True. MyVar = Null ' Assign Null. MyCheck = IsEmpty(MyVar) ' Returns False.


2 Answers

String.isEmpty() was added in Java 6. In earlier versions you can use StringUtils.isEmpty(String) from Apache's commons-lang library.

To configure Eclipse to use the 1.6 JRE, go to Window->Preferences->Java->Installed JREs. If you haven't already got JAva 1.6 configured, select Add...,browse to your 1.6 installation and add it.

installed JREs screenshot

configure JREs screenshot
(source: lumidant.com)

like image 184
Rich Seller Avatar answered Sep 28 '22 09:09

Rich Seller


If you're not on Java 6, String.length() == 0 will return the same result as String.isEmpty().

like image 34
dogbane Avatar answered Sep 28 '22 09:09

dogbane