what wrong whit this statment:
while(!code.equals(code1||code2||code3))
{
System.out.print("Ivalid code. Please enter code again: ");
code = in.nextLine();
}
Error msg: bad operand types for binary operator
thanks :))
Printable View
what wrong whit this statment:
while(!code.equals(code1||code2||code3))
{
System.out.print("Ivalid code. Please enter code again: ");
code = in.nextLine();
}
Error msg: bad operand types for binary operator
thanks :))
If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're talking about. As of now we'd just be guessing. Don't forget the highlight tags.
My problem is that i wish that the while loops keeps looping until the 'code' inputted is equal to one of the pre-defined code: code1, code2, code3.
System.out.print("CODE: ");
code = in.nextLine();
int quantity=0;
while(!code.equals(code1||code2||code3))
{
System.out.print("Ivalid code. Please enter code again: ");
code = in.nextLine();
}
So i guess !code.equals(code1||code2||code3 is not excepted by the program
Again, without knowing exactly what you're talking about, I'm only guessing (and where are your highlight tags?), but I believe your problem lies in the syntax of a conditional:
DON'T DO THIS: if(one == two || three)
DO THIS INSTEAD: if(one == two || one == three)
I'm using one, two, three, if, ||, and == for demonstrative purposes, but the lesson is this: && and || separate individual, full expressions, not just lists of values.
Sry for not being very clear to explain my problem. Hope i improve with time hehe.
Ok thanks for your help. I knew that it works in this way but i thought that maybe another method might exist to save the user some lines. What if you have to compare with 50 other integers? Do you have to write the '!code.equals(coseX)' 50 times?
Thanks again!
Yeah, you would, however it seems that you should be checking an array, not a ton of variables. If you put code1, code2, code3....code50 all in a list, you could just do this:
Code Java:/* * PRECONDITION: list is not null and is of the same type of i, * which is also not null. */ Collections.sort(list); int ind = Collections.binarySearch(list, i); if(ind >= 0) { System.out.println(i+" is in "+list+" at index "+ind); }else { System.out.println(i+" is not in "+list); }
If it is for an array, u could also use a "for" or a do{...}while()
Or you could do what I just said, which would have less code and most likely be more efficient.