-2

It's really easy to get the length of a single string and return it's value!

public static int length(String s){//trys and returns length of any string
    try{//try code
    len = s.length();//gets the length of any string
    }catch (NullPointerException nullError){//checks for nulls
        len = 0;//if there is a null then there is 0 length of the string
    }
    return len;//return the integer length of any string
}

This simple code will catch an error! An example of an error would be if you use a JOptionPane and the user doesn't print anything just clicks the X. That would cause a null error! So if that's the case then then length would just be 0!

But now I just want to know how to make this a shorter code!

  • 5
    and your question is? – Amer Qarabsa Oct 13 '16 at 06:24
  • One of my friends need to know this so I posted this for them and anyone else that wanted to use this! @AmerQarabsa – Dylan Baldwin Oct 13 '16 at 06:28
  • 1
    Welcome to Stack Overflow! Have a look at the [How to Ask](http://stackoverflow.com/help/how-to-ask) page to get some tips on how to get the most of the SO community, and what makes for good questions and answers. This particular format isn't really appropriate as a question, since it isn't presenting a problem that someone can answer. Check out the Help Center for tips on getting started. Good luck! – nbrooks Oct 13 '16 at 06:30

3 Answers3

2

return str != null ? str.length() : 0

nbrooks
  • 18,126
  • 5
  • 54
  • 66
Gikkman
  • 752
  • 6
  • 21
  • Thank you! I need to learn more! – Dylan Baldwin Oct 13 '16 at 06:31
  • No problem, everyone's new at some point. Please remember to accept the answer if it solved your problem. And also, take a minute to read the [Tour](http://stackoverflow.com/tour) if you are new here. Welcome to SO :-) – Gikkman Oct 13 '16 at 06:34
0

There is a misconception on your end.

A null string is null. It doesn't have a length! An empty string has length 0.

And to add more confusion, try what happens when you do

String str = null;
System.out.println(str);

Surprise ... that will print more than just an empty line.

In other words: better think twice before defining that a null string has length 0!

In that sense, you might better be doing something like

public static String ensureNotNull(String input) {
  if (input == null) { return ""; }
  return input;
}

String someString = ... coming from somewhere, maybe null
someString = ensureNotNull(someString);

Because that will make sure that someString has length 0, and is not null! More importantly: "your" idea is to have A) some string variable B) some additional int variable. Meaning: based on the value of that string, you have to carry along that additional int value. So, all of a sudden, your code dealing with that one string ... also has to deal with that other int value. All the time. Not a good idea.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
-1
public static int length(String s){
   return s==null?0:s.length();
}
Noushad
  • 384
  • 3
  • 11