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
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
mvonb17 (February 6th, 2012)
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:
/* * 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.