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.

Results 1 to 8 of 8

Thread: I am a beginner please help :)

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I am a beginner please help :)

    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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I am a beginner please help :)

    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!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I am a beginner please help :)

    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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I am a beginner please help :)

    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!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mvonb17 (February 6th, 2012)

  6. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I am a beginner please help :)

    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!

  7. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: I am a beginner please help :)

    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);
    }

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: I am a beginner please help :)

    If it is for an array, u could also use a "for" or a do{...}while()

  9. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: I am a beginner please help :)

    Or you could do what I just said, which would have less code and most likely be more efficient.

Similar Threads

  1. Beginner
    By codejava in forum Member Introductions
    Replies: 2
    Last Post: August 22nd, 2011, 08:11 AM
  2. Beginner
    By angelo24 in forum Member Introductions
    Replies: 1
    Last Post: August 19th, 2011, 07:14 AM
  3. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 02:00 PM
  4. Programming beginner
    By LatinaC09 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 01:33 PM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM