Why can't I use || or && with double as in sample below?

 if (((mDiameter = 1 || mDiameter = 1.2) && mPitch = 0.25) || (mDiameter = 1.6 && mPitch = 0.35) || (mDiameter = 2 && mPitch = 0.4)
                || (mDiameter = 2.5 && mPitch = 0.45) || (mDiameter = 3 && mPitch = 0.5) || (mDiameter = 4 && mPitch = 0.7)
                || (mDiameter = 5 && mPitch = 0.8) || (mDiameter = 6 && mPitch = 1) || (mDiameter = 8 && mPitch = 1.25)
                || (mDiameter = 10 && mPitch = 1.5) || (mDiameter = 12 && mPitch = 1.75) || (mDiameter = 16 && mPitch = 2)
                || (mDiameter = 20 && mPitch = 2.5) || (mDiameter = 24 && mPitch = 3) || (mDiameter = 30 && mPitch = 3.5)
                || (mDiameter = 36 && mPitch = 4) || (mDiameter = 42 && mPitch = 4.5) || (mDiameter = 48 && mPitch = 5)
                || (mDiameter = 56 && mPitch = 5.5) || (mDiameter = 64 && mPitch = 6)) {

mDesignation = "METRIC COARSE - 2nd CHOICE";

        } else if (mDiameter = 11 && mPitch = 1.5)

        {
            mDesignation = "METRIC COARSE - 3rd CHOICE";

.......................................... etc

What? Besides the fact that this is a terrible piece of code? You are mixing doubles, integers, and floating point values. You need to cast everything to a double to compare them, otherwise the math co-processor will mis-interpret what you want.

And that is just the beginning of your problems... There are things like the fact that floating-point values on computer hardware aren't always exactly what you think. The fact is, 1.1 may be represented in hardware as 1.1000000001 or something like that. Caveat programmer!

Comparing integers? Not such a problem. Floating point numbers? Gah!

This code was coverted from VB by simply replacing the 'And's and 'Or's. it all worked perfectly there.
Being a beginner I was simply looking for a bit of help or constuctive critiscism.

I have to agree with rubberman, this is ugly code. Besides, you need to use == instead of = here. = is assignment and == is used for comparison.

When work with float-point, we don't usually use == as well because float-point cannot represent certain number. Usually, when compare float point, we need to allow room for some error. For example:

 double errorMargin = 0.00001;
 if (abs(a - b) < errorMargin) // check whether a and b is equal
 { ... }

Ditto.
If all the values are held to 2 decimal places then consider using int variables in units of 1/100. (ie replace float 1.55 with int 155). Then, and only then, can you compare for equals. (And don't forget = is assignment, == is equality operator)

Looking at that code it seems to be crying out for some Object Oriented love, Why not create a Thread class with pitch, diameter as attributes and methods like getDesignation()(with Designation as an enum) or isCompatibleWith(Thread other) etc?

Thanks, I'll go off and try your suggestions.

This code was coverted from VB by simply replacing the 'And's and 'Or's.

One thing to note is that these are not comparable operators. And and Or are not short circuiting in VB, yet && and || are in Java. Whether this is relevant depends heavily on your logic, but it's a point that needs to be made. && and || are only directly equivalent to AndAlso and OrElse.

OK, this works so any chance that I can now have hints or suggestions as to making it more beatiful!
The mDiameter and mPitch are input at earlier activity screen and can be any value. Relevant mDesignation only needs to be applied if conditions are met.

        String mDesignation;


            if (((mDiameter == 1 || mDiameter == 1.2) && mPitch == 0.25) || (mDiameter == 1.6 && mPitch == 0.35) || (mDiameter == 2 && mPitch == 0.4)
                    || (mDiameter == 2.5 && mPitch == 0.45) || (mDiameter == 3 && mPitch == 0.5) || (mDiameter == 4 && mPitch == 0.7)
                    || (mDiameter == 5 && mPitch == 0.8) || (mDiameter == 6 && mPitch == 1) || (mDiameter == 8 && mPitch == 1.25)
                    || (mDiameter == 10 && mPitch == 1.5) || (mDiameter == 12 && mPitch == 1.75) || (mDiameter == 16 && mPitch == 2)
                    || (mDiameter == 20 && mPitch == 2.5) || (mDiameter == 24 && mPitch == 3) || (mDiameter == 30 && mPitch == 3.5)
                    || (mDiameter == 36 && mPitch == 4) || (mDiameter == 42 && mPitch == 4.5) || (mDiameter == 48 && mPitch == 5)
                    || (mDiameter == 56 && mPitch == 5.5) || (mDiameter == 64 && mPitch == 6)) {

                mDesignation = "METRIC COARSE - 1st CHOICE";


            } else if ((mDiameter == 1.1 && mPitch == 0.25) || (mDiameter == 1.4 && mPitch == 0.3) || (mDiameter == 1.8 && mPitch == 0.35) || (mDiameter == 2.2 && mPitch == 0.45)
                    || (mDiameter == 3.5 && mPitch == 0.6) || (mDiameter == 4.5 && mPitch == 0.75) || (mDiameter == 7 && mPitch == 1) || (mDiameter == 14 && mPitch == 2)
                    || (mDiameter == 18 && mPitch == 2.5) || (mDiameter == 22 && mPitch == 2.5) || (mDiameter == 27 && mPitch == 3) || (mDiameter == 33 && mPitch == 3.5)
                    || (mDiameter == 39 && mPitch == 4) || (mDiameter == 45 && mPitch == 4.5) || (mDiameter == 52 && mPitch == 5) || (mDiameter == 60 && mPitch == 5.5)
                    || (mDiameter == 68 && mPitch == 6)) {

                mDesignation = "METRIC COARSE - 2nd CHOICE";

            } else if (mDiameter == 11 && mPitch == 1.5)

            {
                mDesignation = "METRIC COARSE - 3rd CHOICE";

There are many more option after this so it gets even more ugly.

You can create a class (see previous post) and have a searchable List of those standard sizes, something like

class ThreadSize {
    final double diameter, pitch;
    final String designation;
    public ThreadSize(double diameter  etc etc
    ...
    public boolean matches(double diameter, double pitch) {
        return diameter == this.diameter && pitch == this.pitch
    }
    ...
}


List<ThreadSize> standardSizes = new ArrayList<>();
standardSizes.add(new ThreadSize(1.0, 0.25, ""METRIC COARSE - 1st CHOICE"));
standardSizes.add(new ThreadSize(1.2, 0.25, ""METRIC COARSE - 1st CHOICE"));
...
standardSizes.add(new ThreadSize(11, 1.5, ""METRIC COARSE - 3rd CHOICE"));

...

String getDesignation(double mDiameter, double mPitch) {
   for (Thread t : standardSizes) {
      if (t.matches(mDiameter, mPitch) return t.designation;
   }
   return ";
}

Thanks James, I'm working on this gradually. I've posted a linked question titled 'SQLite3 database or list?' which may be another way around this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.