• Post Reply Bookmark Topic Watch Topic
  • New Topic

removing a character from string  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to remove a character from string as below
'

what is the best way to remove it.
If i call like
removeChar("hello", 1) i should see hllo.

When there are more than one way to do a challenge in java which one to choose as best way.
What is the criteria to decide one particular way of writing code is best among say 'n' different number of ways.
Please advise. thanks in advance


 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use String#substring for this.
Your method is not good because.
- It would replace all characters from given index so reomveChar("aba", 0) would return "b" instead of "ba".
- But as you wrote it, it will remove all characters "o", so reomveChar("oabo", 1) would return "ab" instead of "obo".

And you have a typo in method's name. It should be removeChar, not reomveChar.

Also, I don't like this returning null. Why not throw IllegalArgumentException, or even better StringIndexOutOfBoundsException?

Also, n<=(str.length()-1) can be rewritten as n < str.length(). Doesn't it look better?

Also, do not use & for logical AND unless you really need this. Use && instead.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I rewrote as above
StringIndexOutOfBoundsException is thrown when the n is out of range which makes sense.
When does IllegalArgumentException is thrown?
Instead of null what else i can return as the method is forcing to return somethign out of 'if' condition

n<=(str.length()-1) can be rewritten as n < str.length(). Definitely looks better. Where can i see the list of effective ways of writing code. Is there is a link explainging the common mistakes and preferred ways of writing them. Please advise
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you put your String into a StringBuilder(← link) and remove the char at 1. Then call toString to get the new String.
Strings are immutable and not intended for changing. StringBuilder is the mutable counterpart of String and is intended for changing.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Both IllegalArgumentException and StringIndexOutOfBoundsException are unchecked exceptions.
You do not need to declare them via throws clause.

2. You have declared that those exceptions can be thrown but you neved actually threw them.

3. No need to throw them by yourself. String#split already does that for you if the argument is illegal.

4. Well, you don't need to return anything in case an exception is thrown. A non-void method must do one of two things. Return a value or throw an exception. So return null is not needed.

5. Do not put a comma after second exception (before curly brace).

Except for that your method returns correct values .
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
When does IllegalArgumentException is thrown?

Did you check the API? It tells you: "[IllegalArgumentException is t]hrown to indicate that a method has been passed an illegal or inappropriate argument."

What arguments are you passing in that gives this exception? it should tell you exactly where the exception is thrown. Print out the arguments just before calling the method to see that they really are what you think they are.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I modified code as above.

"String#split already does that for you if the argument is illegal"
what is the meaning of 'String#split ' and how it dies automatically.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read about String#split here.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did I say String#split?
Omigod, I meant String#substring.
Sorry that I confused you.

You are still returnig null on illegal index. Don't do that.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"You are still returnig null on illegal index. Don't do that."


what i have to do to so that i wont send illegal index and get null as return. Please advise
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let String#substring throw an exception when illegal index is passed.

That's what I would do.

Another option would be to return the original String.
One can argue that if you have to remove a character with index -3 then no such character can be found therefore no character will be removed so the original String should be returned.

This really depends on what your requirements are.

In my opinion returning null is the worst possible thing to do. If you do not permit illegal indexes it is best to fail fast and fail loud.
If you don't fail fast you could see NullPointerException in some unralated method in the stack trace and spend hours to find the original cause.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So simply write as above with null right. Does the method expect return type outside the if condition loop?

What is fail fast, loud. How to do that. please advise
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will fail fast and fail loud. As soon as you pass null, you will suffer an exception form the length() method.
Better to throw the Exception specifically. Look at this thread from last week.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so passing null means passing null for str(not for the n) as below right.

removeChar("", 1) → "ktten"
Above line is passing null or empty string. Does null and empty string means same thing?

I was checking other post comments I see as below

Effective Java wrote:
NullPointerException : Parameter value is null where prohibited.
IllegalArgumentException : Non-null parameter value is inappropriate.



what it means by 'Parameter value is null where prohibited. ' what it means by prohibited? Please advise

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Passing null and passing an empty String mean completely different things. you should avoid letting nulls anywhere near your code if at all possible.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!