d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question
Add Reply New Topic New Poll
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Jan 11 2015 12:56pm
Ive never personally used this in my code but i would like to know what this is doing or some sort of explanation please :)

result = Character.isUpperCase(n.charAt(0)) ? 1 : 0;

the portion: ? 1 : 0;

what does that do?
Code
public static int countUpper(String n){
assert n.isEmpty() == false;
int result=0;

if (n.length() == 0)
result = 0;
else
result = Character.isUpperCase(n.charAt(0)) ? 1 : 0;
return countUpper(n.substring(1)) + result;
}


If you're curious, the assignment is to count uppercase letters in a string recursively
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Jan 11 2015 01:03pm
Quote (carteblanche @ 11 Jan 2015 14:02)


Ill read this through thank you very much!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 11 2015 01:21pm
I really don't like your code, btw. You're abusing assert to check params and i think your substring call fails if the string is too small.

http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#usage

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)

This post was edited by carteblanche on Jan 11 2015 01:22pm
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Jan 11 2015 01:33pm
Quote (carteblanche @ 11 Jan 2015 14:21)
I really don't like your code, btw. You're abusing assert to check params and i think your substring call fails if the string is too small.

http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#usage

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)


Code
public static int countUpper(String n){
assert n.isEmpty() == false;
int result;
if (n.length() == 0)
return 0;
result = Character.isUpperCase(n.charAt(0)) ? 1 : 0;
return countUpper(n.substring(1)) + result;
}


changed it up before you posted. Its mildly lazy i know, the professor was more concerned with the recursion than anything. He did say however error handling could be done using either assert or throwing an exception...so if in this instance assert is a bad choice i can always change it

And i guess im confused by what you mean about failing if the string is too small?

This post was edited by Wacko on Jan 11 2015 01:35pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 11 2015 02:01pm
Quote (Wacko @ Jan 11 2015 02:33pm)
Code
public static int countUpper(String n){
        assert n.isEmpty() == false;
        int result;
        if (n.length() == 0)
            return 0;
        result = Character.isUpperCase(n.charAt(0)) ? 1 : 0;
        return countUpper(n.substring(1)) + result;
    }


changed it up before you posted. Its mildly lazy i know, the professor was more concerned with the recursion than anything. He did say however error handling could be done using either assert or throwing an exception...so if in this instance assert is a bad choice i can always change it

And i guess im confused by what you mean about failing if the string is too small?


as for assert, it makes no sense to have it there. it's perfectly valid to have an empty string as input and return 0. in fact, it's necessary to have it there as your base case of the recursive method.

Quote
assert n.isEmpty() == false;
        if (n.length() == 0)


you're asserting that it's not empty, but then immediately after you're checking if it's empty. that's a logical error.

as for parameter validation, it should be done using exceptions. eg if the user inputs null into your function, you're going to get a null pointer exception.

as for the substring:

From the link i posted:
Quote
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.


from your code:
Quote
return countUpper(n.substring(1)) + result;


in your original code, this would fail if the length is 0. in your new code you fixed it by returning 0 before it gets here. i assume you found that by running it.
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Jan 11 2015 02:04pm
Quote (carteblanche @ 11 Jan 2015 15:01)
as for assert, it makes no sense to have it there. it's perfectly valid to have an empty string as input and return 0. in fact, it's necessary to have it there as your base case of the recursive method.



you're asserting that it's not empty, but then immediately after you're checking if it's empty. that's a logical error.

as for parameter validation, it should be done using exceptions. eg if the user inputs null into your function, you're going to get a null pointer exception.

as for the substring:

From the link i posted:


from your code:


in your original code, this would fail if the length is 0. in your new code you fixed it by returning 0 before it gets here. i assume you found that by running it.


Sent you a PM, but for the sake of anyone else who has this problem... The logic was flawed you are right, trying to catch a empty string shouldn't be done inside of my recursive call which will eventually have an empty substring.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll