• Post Reply Bookmark Topic Watch Topic
  • New Topic

Determine if the string contains either x or z  RSS feed

 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use charAt method to find the character at any index and then compare it
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aaron butcher wrote:



Your code itself has some disadvantages:

- it is not formatted, it you are using Eclipse please press Ctrl+A then Ctrl+Shift+F
- you do not check the input to see if it is valid (text can be null or empty)
- you create a useless found (result) value, it makes code way harder to understand
- you create the "lower" String, it cost resources to turn all of text's characters to lowercase, thus if text.charAt(0) == 'x' it becomes wasteful
- string letter is only used on the next line, thus you do not need to create a new variable for it

Having said that, the solution you chose is more elegant that using charAt(), I do like your "xzXZ"contains() approach. Here is your code a little bit more formatted:

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You missed the greatest disadvantage of the code. When you start seeing things with bang signs in, you should start getting suspicious. So I looked, and, lo and behold, a loop which never starts.

You have also gone against the policies of this forum. Look what it says on the title page for this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
That is exactly what you have done. So, don't be annoyed, but I have pulled rank and deleted the solution.

I would say both solutions are inelegant because they use more than one return. I would suggest that correct use of the loop and the found local variable will enable you to find the letters and only use return as the very last line of the method. I think validation is something which can be left until after the loop is working; you obviously consider null to be valid input, Lucian Whiteman.
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i did a simple loop instead of the while loop just would produce what i wanted so i used.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't a loop, but an if statement, sometimes called a selection statement or a conditional statement. There is a stylistic improvement which you can make if you find the old Sun style conventions (try §10.5.2).

That sort of thing will work all right if you only have a few options to search for, but will become unmanageable if you have many options. Oddly enough somebody else had a similar question only a few weeks ago. Reading that thread might help you. I was told off for suggesting something complicated.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of you could use
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi aaron,

Please do not mind the furstrations of some admin that deletes my posts. He is upset because I once told him he was wrong about some java issue.

Here is a piece of code that may help you:

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucian Whiteman wrote: . . .
Please do not mind the furstrations of some admin that deletes my posts. He is upset because I once told him he was wrong about some java issue.
. . .
Lots of people tell me I am wrong. But I am charged with maintaining the policies of this website.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucian Whiteman wrote:

Please do not mind the furstrations of some admin that deletes my posts. He is upset because I once told him he was wrong about some java issue.



Even if that were true (which I'm sure it isn't), you still broke one of the rules of this site.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shall we get back to the proper theme of the discussion, please.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!