d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need This Code
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 3 2017 05:21pm
Create a method that returns a boolean given a string, using if statements to determine of the string is bigger than or smaller than 5 characters
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Nov 3 2017 05:59pm
Read this: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Code

public boolean isOverFiveCharacters(String str) {
if(str.length() > 5) {
return true;
}
return false;
}
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 3 2017 06:25pm
Quote (waraholic @ Nov 3 2017 07:59pm)
Read this: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Code
public boolean isOverFiveCharacters(String str) {
if(str.length() > 5) {
return true;
}
return false;
}


Thanks so much!
Question thoguh, how come you don't use "else" for return false?
Member
Posts: 3,197
Joined: May 4 2013
Gold: 1,457.00
Nov 3 2017 07:26pm
You don't need to.

The if clause will return true if the statement is true, or program will continue and encounter return false. This is more elegant, and in more complicated functions it can prevent undefined result
Member
Posts: 1,423
Joined: Dec 9 2011
Gold: 6.94
Nov 4 2017 03:45am
Quote (waraholic @ 4 Nov 2017 00:59)
Read this: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Code
public boolean isOverFiveCharacters(String str) {
if(str.length() > 5) {
return true;
}
return false;
}


if you don't check if str is not null you can get a nullpointerException at str.lenght()

you should do :

if(str != null && str.length() > 5) {

This post was edited by MexG on Nov 4 2017 03:48am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 4 2017 03:30pm
Quote (MexG @ Nov 4 2017 04:45am)
if you don't check if str is not null you can get a nullpointerException at str.lenght()

you should do :

if(str != null && str.length() > 5) {


Let's just be completely pedantic then:

Code

public boolean isOverFiveCharacters(String s)
{
return java.util.Optional.ofNullable(s).orElse("").length() > 5;
}
Member
Posts: 1,423
Joined: Dec 9 2011
Gold: 6.94
Nov 4 2017 05:48pm
Code
public boolean isOverFiveCharacters(String s)
{
return java.util.Optional.ofNullable(s).orElse("").length() > 5;
}


he wanted if statments ^^

Quote (ferf @ 4 Nov 2017 00:21)
Create a method that returns a boolean given a string, using if statements to determine of the string is bigger than or smaller than 5 characters
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Nov 5 2017 10:04am
Quote (MexG @ Nov 4 2017 04:45am)
if you don't check if str is not null you can get a nullpointerException at str.lenght()

you should do :

if(str != null && str.length() > 5) {


That is completely unnecessary imho. Let it throw a NPE and add that to the javadoc.

If your method is getting passed null parameters there are likely issues in the invoking method. Checking for NPE everywhere makes code look horrible and it doesn't fix the problem. There is likely a problem in the calling class if you're passing null.
Member
Posts: 7,206
Joined: May 11 2009
Gold: 5.00
Nov 9 2017 07:52pm
Code
public boolean isOverFiveCharacters(String s)
{
return s == null ? false : s.length() > 5;
}


Another variation using ternary that is also null safe.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll