We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to compare and sort two differenent strings.
Page Index Toggle Pages: 1
How to compare and sort two differenent strings. (Read 1051 times)
How to compare and sort two differenent strings.
Dec 21st, 2009, 5:15am
 
Hi There.

So, if I have a string such as:
Code:

String num = "1233445.023";


Is there a simple way to check if the String has a "." in it?

I am writing a simple calculator and want to make sure the user can only enter a maximum of one "." when typing in the numbers.

the only idea I have is to use toCharArray() and then loop through it each time it's updated with the substring method to see if it has more than one "." in it.

My intuition tells me I'm over-complicating things. Any other suggestions??

Thanks,

Dan.
Re: How to compare and sort two differenent strings.
Reply #1 - Dec 21st, 2009, 6:14am
 
you can use indexOf to check if a particular substring is contained inside a String. in your case the "."

Code:

String num = "1233445.023";
println(num.indexOf(".")); // prints 7

here num.indexOf(".") will return 7 - the index of the substring you have been looking for. if a string does not contain the substring you are looking for, -1 will be returned.
Code:

String num = "1233445";
println(num.indexOf(".")); // prints -1


Re: How to compare and sort two differenent strings.
Reply #2 - Dec 21st, 2009, 6:19am
 
as you want to check if you have one or more. you then have to create a substring from this position and check if it still contains a "."
Re: How to compare and sort two differenent strings.
Reply #3 - Dec 21st, 2009, 6:44am
 
This should do the trick:

Code:

boolean containsAtMostOneDot(String s)
{
return s.indexOf(".") == s.lastIndexOf(".");
}
Re: How to compare and sort two differenent strings.
Reply #4 - Dec 21st, 2009, 6:48am
 
You only want to check if you already have single point in the string (no need to check for more) - and then simply not allow another point to be added if there is (though I think that's what you meant in the first place).

So assuming your numbers are being stored in a string buffer* as they're added (i.e. until an operator is selected), simply check for the presence of a decimal point in this buffer when they hit the '.' button  (see Reference > String Functions > maybe 'match()'?).  If there's already one there don't add another point to the buffer.

That's how I would do it anyway...

* e.g. something like this rather than an array:
String foo;
foo += inputString;

IIRC as long as 'inputString' is indeed a string it should be concatenated to the existing string...
Re: How to compare and sort two differenent strings.
Reply #5 - Dec 21st, 2009, 8:04pm
 
Thanks for the speedy resonse guys!

Both examples are far more effecient than what I had in mind. match() sounds ideal except for one problem I found with it.

In my case I am seaching for "."

here's and example using match()
Code:

String s1 = "12394802";

String[] m1 = match(s1, ".");
if (m1 != null) {
 // This will print to the console.
 println("Found a match in '" + s1 + "'");  
} else {
 println("No match found in '" + s1 + "'");
}


the problem is that "." seems to be some sort of special search character. According to this http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html it means "Any Character" Sad

I'm out of my depth here when it comes to expressions and java.

I think I will go with sojamo's example unless anyone can suggest a way to force match() to see "." as a regular character?

Thanks again guys,

You are always very helpful.

Dan.
Re: How to compare and sort two differenent strings.
Reply #6 - Dec 21st, 2009, 10:37pm
 
If you are trying to see if there is a dot present, match() is overkill. I recommend using the code I posted above or another variant using indexOf.

But to answer the question, you have to escape the dot: match(s1, "\\.");

Edit:
I've just done a small speed test, if you don't need other functionality from match (regexes, grouping, ...) you really should use indexOf, it's roughly 35 times faster (depending on the input of course)
Re: How to compare and sort two differenent strings.
Reply #7 - Dec 21st, 2009, 10:55pm
 
Great thanks JR,

I have been using indexOf and it does the trick. Thanks for doing the speed test too!

Dan.
Re: How to compare and sort two differenent strings.
Reply #8 - Dec 22nd, 2009, 12:53am
 
FYI here's what I'm working on:
http://www.openprocessing.org/visuals/?visualID=6650

Cheers!
Page Index Toggle Pages: 1