• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

String check

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic