• Post Reply Bookmark Topic Watch Topic
  • New Topic

String check  RSS feed

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a requirement where, I need to check the string for the presence of = and &. If these two are present, then, Print, String is valid.
Else I need to print string is invalidHow to frame the design for this?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't exactly understand your requirements, because what you describe in text doesn't seem to match your examples.

You say: If = and & are present then the string is valid, otherwise it is invalid. Then you have an example: "Hellowrld" is valid. How can this be valid? It contains neither = nor &. And how does your requirement explain that "hello!world" is invalid?

First, make sure that you know exactly what valid or invalid means. If you can't exactly describe this, then you can't make a program that implements the rules. If the rules are clear, then you can probably do this with a regular expression.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:Hi I have a requirement where, I need to check the string for the presence of = and &. If these two are present, then, Print, String is valid.
Else I need to print string is invalid

Presuming you mean the characters '=' and '&', use String.indexOf().

Winston
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper+1 except I think regex can be avoided.

1) Figure out if the string contains (hint) the required character(s)
2) Figure out if all rules are satisfied
3) Process accordingly
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:Else I need to print string is invalid...

Anu, I broke up that very long line inside your code block; they screw up the window formatting here.

Winston
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the reply.

My requirement: 1. Only string-valid, string with = valid and String with & valid. Any special chars other than = & in the string is invalid.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What’s a “special” char? You need to be specific about your requirements. If you work that out, you may be able to use methods of the Character class (or a regex) to exclude those chars.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:My requirement: 1. Only string-valid, string with = valid and String with & valid. Any special chars other than = & in the string is invalid.

As Campbell (and others) have said: you must learn to describe your requirements precisely.

The sentence "Only string-valid, string with = valid and String with & valid" could mean:
Any string with ANY character that is not a '=' or a '&' is invalid.
which would make the string "HelloWorld" invalid because NONE of its characters are '=' or '&'.

You then go on to say:
"Any special chars other than = & in the string is invalid."
so, as Campbell said, you have to explain what "special chars" means (and don't forget about blanks (' ')).

When I was at school, back before the K-T extinction event, my prof used to say:
Imagine that you have to describe the problem to a 12-year old who has no knowledge of computers.
(tells you how long ago it was)

How would you explain it to him/her?

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!