473,387 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 2117

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: could ildg | last post by:
I have learned python for over a month. I heard that it was very easy to learn, but when I tried to know OO of python, I found it really weird, some expressions seem very hard to understand, and I...
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
4
by: Lucas Sain | last post by:
Hi, I think thta for this I have to use reflection... but I'm not shure. How can I find/get an object at runtime by looking for its name that is stored in a variable. For example: I have a...
3
by: aaa | last post by:
I fail to see the connection between the code behind and the raw HTML of the ASPX page how do you get variables and functions to communicate with each other? I cannot get this to even fire:...
3
by: markaelkins | last post by:
Hi. I am trying to enter a variable in the treenodesrc of a treenode. I am basically trying to send an ID variable into sql to return different records. I've searched everywhere and cannot find the...
3
by: Tom McL. | last post by:
Is there a way I identify and remove unused variable within vb2005 standard edition
5
by: rover8898 | last post by:
Hello all, I have a programming problem/challenge. Let explain my scenario: I have a C program (distributed accross many files and functions) that is responsible for handling hardware. This...
7
by: Zhang Weiwu | last post by:
Dear all How does javascript realiablily tell if a variable is a reference to an HTML Element "<select>", without causing browser to pop up error message? if (variable.constructor ==...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.