By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

how: if variable != [1..6]

P: n/a
I am farly new to java and am interested in how to cover a 'range'.

ie. if variable != [1..6]

cheers,
Dave
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.541 / Virus Database: 335 - Release Date: 14/11/2003
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Dave" <bigdavepotnoodle*SPAM*hotmail.com> wrote in message
news:3f***********************@news.dial.pipex.com ...
I am farly new to java and am interested in how to cover a 'range'.

ie. if variable != [1..6]


// 1.
if (variable >= 1 && variable <= 6)
{
// Do in-range stuff ...
...
}

// 2. [For numeric, integer-based primitive types ('char' too) only]
switch (variable)
{
case 1: case 2: case 3:
case 4: case 5: case 6:
// Do in-range stuff ...
break;
...
}

// 3. Use 'Set' collection

// a) Previously set up
Set s = new HashSet();
s.add(new Integer(1));
...
s.add(new Integer(6));
...
// b) Perform pseudo 'range' test [actually a membership test
// but it serves the required purpose]
if (s.contains(new Integer(variable)))
{
// Do in-range stuff ...
...
}

// 4. Iterate through a range [e.g. array or collection]
// testing for element membership ...
int[] array = new int[100];

array[1] = 1;
array[2] = 2;
array[3] = 3;
array[4] = 4;
array[5] = 5;
array[6] = 6;

int variable = 5;
boolean found = false;

for (int i = 0; i < array.length && !found; ++i)
found = (array[i] == variable);

if (found)
{
// Do in-range stuff ...
...
}

// 5. Use 'BitSet'
BitSet bs = new BitSet(100); // Range of 0 .. 99

bs.clear(0);
bs.set(1, 6); // Set values 1..6
bs.clear(7, 99);

if (bs.get(variable))
{
// Do in-range stuff ...
...
}

This is similar to doing:

boolean[] array = new boolean[100];

array[1] = true;
array[2] = true;
array[3] = true;
array[4] = true;
array[5] = true;
array[6] = true;
...
int variable = 5;
boolean found = array[variable];

if (found)
{
// Do in-range stuff ...
...
}

The third, fourth, and fifth examples are probably not what you want but are
shown for sake of illustration. If you try to 'think outside the square',
*much* is possible using Java :) !

I hope this helps.

Anthony Borla
Jul 17 '05 #2

P: n/a
if(variable < 1 || variable > 6){
// your code
}

Patrick

Dave wrote:
I am farly new to java and am interested in how to cover a 'range'.

ie. if variable != [1..6]

cheers,
Dave
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.541 / Virus Database: 335 - Release Date: 14/11/2003


Jul 17 '05 #3

P: n/a
Cheers, do i feel like a 'doof'

(i think top posting can be excused this once!)

"Anthony Borla" <aj*****@bigpond.com> wrote in message
news:4t*****************@news-server.bigpond.net.au...

"Dave" <bigdavepotnoodle*SPAM*hotmail.com> wrote in message
news:3f***********************@news.dial.pipex.com ...
I am farly new to java and am interested in how to cover a 'range'.

ie. if variable != [1..6]

// 1.
if (variable >= 1 && variable <= 6)
{
// Do in-range stuff ...
...
}

// 2. [For numeric, integer-based primitive types ('char' too) only]
switch (variable)
{
case 1: case 2: case 3:
case 4: case 5: case 6:
// Do in-range stuff ...
break;
...
}

// 3. Use 'Set' collection

// a) Previously set up
Set s = new HashSet();
s.add(new Integer(1));
...
s.add(new Integer(6));
...
// b) Perform pseudo 'range' test [actually a membership test
// but it serves the required purpose]
if (s.contains(new Integer(variable)))
{
// Do in-range stuff ...
...
}

// 4. Iterate through a range [e.g. array or collection]
// testing for element membership ...
int[] array = new int[100];

array[1] = 1;
array[2] = 2;
array[3] = 3;
array[4] = 4;
array[5] = 5;
array[6] = 6;

int variable = 5;
boolean found = false;

for (int i = 0; i < array.length && !found; ++i)
found = (array[i] == variable);

if (found)
{
// Do in-range stuff ...
...
}

// 5. Use 'BitSet'
BitSet bs = new BitSet(100); // Range of 0 .. 99

bs.clear(0);
bs.set(1, 6); // Set values 1..6
bs.clear(7, 99);

if (bs.get(variable))
{
// Do in-range stuff ...
...
}

This is similar to doing:

boolean[] array = new boolean[100];

array[1] = true;
array[2] = true;
array[3] = true;
array[4] = true;
array[5] = true;
array[6] = true;
...
int variable = 5;
boolean found = array[variable];

if (found)
{
// Do in-range stuff ...
...
}

The third, fourth, and fifth examples are probably not what you want but

are shown for sake of illustration. If you try to 'think outside the square',
*much* is possible using Java :) !

I hope this helps.

Anthony Borla

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.541 / Virus Database: 335 - Release Date: 14/11/2003
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.