• Post Reply Bookmark Topic Watch Topic
  • New Topic

if string starts with certain character, or misses certain characters, as a condition in in if()  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use specific characters, or the lack of certain characters, in a string as a condition in an if statement?
And if yes, how would I accomplish that?

I have an assignment where in a Method I have to convert a string representation of an array ( {{1.0, 2.0}, {3.0, 4.0}} ) into a double[] [] array.
I have to also make sure that the method returns 0 IF the String input is not in the exact format I gave in the example above.

Is it possible with what I wrote in the beginning?
 
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
Sure. Class String has a contains() method, which you can use to check if a string contains a certain substring. See the API documentation of class java.lang.String.

For more complicated things, such as checking if a string contains data in a specific format, you can use a regular expression.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:Is it possible to use specific characters, or the lack of certain characters, in a string as a condition in an if statement?
And if yes, how would I accomplish that?

I have an assignment where in a Method I have to convert a string representation of an array ( {{1.0, 2.0}, {3.0, 4.0}} ) into a double[] [] array.
I have to also make sure that the method returns 0 IF the String input is not in the exact format I gave in the example above.

Is it possible with what I wrote in the beginning?

Not quite sure what you're asking here; but if it's: "does a solution for [a string representation for a double array] jive with checking whether a string starts/doesn't start with certain characters"? The answer is probably 'no' .... or if you can find a solution based on it, it will probably be tortuous and brittle.

What you need to do is look hard at that string
{{1.0, 2.0}, {3.0, 4.0}}
and understand what it represents, and what is allowed.
Is, for example:
{ {1.0, 2.0} , {3.0, 4.0}          }
allowed?
And if not, why not?

And what do those outer braces mean?

Regexes are certainly likely to be a help, but understanding the problem is even more important.

HIH

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the question given, yes, you can can use anything as a condition of an if. example:


then, elsewhere in your class, you have to write methods 1 - 3. your methods might be named more like

startsAndEndWithCurlie()
doesNotContainLetters()
doesNotContainSpaces()

The trick is to accurately define what is necessary and sufficient for your needs.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!