• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the minimum value, excluding -1

 
author
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a string and parsing through it, trying to discover the next instance of a single-quote ', a parenthesis ), a new-line \n, or a space.
Basically, I want to find out which one has the lowest number in an indexOf, but if the indexOf == -1, I want to exclude them from the check.
int nLineTest = restOfQry.indexOf("\n");
int quoteTest = restOfQry.indexOf('\'');
int parenTest = restOfQry.indexOf(')');
int spaceTest = restOfQry.indexOf(' ');
int lowestNum = 0;

// Find the location of the end of the parameter in qryText
if (Math.min(Math.min(quoteTest,nLineTest),Math.min(parenTest,spaceTest)) > -1) {
lowestNum += Math.min(Math.min(quoteTest,nLineTest),,Math.min(parenTest,spaceTest));
} else if (quoteTest > -1) {
lowestNum += quoteTest;
} else if (parenTest > -1) {
lowestNum += parenTest;
} else if (spaceTest > -1) {
lowestNum += spaceTest;
} else if (nLineTest > -1) {
lowestNum += nLineTest;
} else {
lowestNum = myQuery.length();
}
Also, if all of the above are equal to -1, then I need to know that too. What's the simplest way to do this, maybe using Math.min or something.
Thanks,
Matt
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Matt.
I don't know if this approach is the best, but it seems a little cleaner than having to mess with Math.min():

Good luck.
Art
[This message has been edited by Art Metzer (edited April 01, 2001).]
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic