6 Replies - 1813 Views - Last Post: 11 August 2010 - 08:35 PM Rate Topic: -----

#1 Guest_cookie*


Reputation:

Conditions in array

Posted 11 August 2010 - 11:31 AM

I have an if statement:
if(Character.isDigit(input.charAt(i)) || input.charAt(i) == '-' || input.charAt(i) == ',' || Character.isLetter(input.charAt(i)) || input.charAt(i) == '\n' || input.charAt(i) == '.')



There are a lot of conditions (and probably a lot more to come). Is there anyway I can put all these conditions into an array?
Is This A Good Question/Topic? 0

Replies To: Conditions in array

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: Conditions in array

Posted 11 August 2010 - 11:48 AM

The standard thing to do here is create a Boolean method to hold your huge condition. Within the method you can break your condition down by creating Boolean variables like isComma or isDash.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Conditions in array

Posted 11 August 2010 - 11:54 AM

You might try a vector and it's contains() method.
Was This Post Helpful? 1
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: Conditions in array

Posted 11 August 2010 - 04:27 PM

n8wxs has given me a great idea. You can easily do this with the String method indexOf like thisL

if (1234567890-,.\n".indexOf('a') != -1) {
}


My guess is you're looping over a string in which case you could use regex and skip the loop:

if (myString.matches("[0-9-,.\\n]+")) {
}


Change the + to a * if you want it to allow an empty string.

Are you trying to make sure the string represents a number? It's possible to write a regex for that, making sure the - isn't in the middle of the number.
Was This Post Helpful? 1
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Conditions in array

Posted 11 August 2010 - 04:43 PM

Cute! :)
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: Conditions in array

Posted 11 August 2010 - 04:46 PM

It was your idea really.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Conditions in array

Posted 11 August 2010 - 08:35 PM

View Postcookie, on 11 August 2010 - 12:31 PM, said:

I have an if statement:
if(Character.isDigit(input.charAt(i)) || input.charAt(i) == '-' || input.charAt(i) == ',' || Character.isLetter(input.charAt(i)) || input.charAt(i) == '\n' || input.charAt(i) == '.')


This makes the performance specialist I am freaks. Calling 6 times the same method in a single statement. At least please do:
char digit = input.charAt(i);
if(Character.isDigit(digit) || digit == '-' || digit == ',' || Character.isLetter(digit) || digit == '\n' || digit == '.')


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1