Results 1 to 4 of 4
  1. #1
    Crazz is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default [SOLVED] "Operator || cannot be applied to java.lang.String,java.lang.String"

    Hi everybody, this is my first post.

    It's kind of hard to say what I am trying to do, so I will show you instead:

    int theInteger1; // an integer
    int theInteger2; // another integer
    String theString; // a string
    String[] theArray;
    theArray = new String[4]; // an array with 4 items
    theArray[0] = "option1";
    theArray[1] = "option2"; // the items in my array
    theArray[2] = "option3";
    theArray[3] = "option4";

    theInteger1 = (int) (Math.floor(Math.random()*4)); // generates a random number between 0 and 3
    theString = theArray[theInteger1]; // sets the string to the object at the position of the random number

    *This is the part where I get stuck*

    if (theString.equals("option1" OR "option2")){
    theInteger2 = 1;
    }
    if (theString.equals("option3" OR "option4")){
    theInteger2 = 2;
    }

    What I want to do is replace the word "OR" in the brackets with a command. I have tried using ||, but this is apparently only used for integers and gives me the error "Operator || cannot be applied to java.lang.String,java.lang.String"

    I hope I have explained this well enough.

    Thanks,
    Crazz
    Last edited by Crazz; 05-02-2011 at 08:26 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please wrap your code in code tags, to do this, do the following
    [code]
    YOUR CODE HERE
    [/code]


    The problem with using || is that it requires booleans, which strings are not. You want to switch it to something like this

    Java Code:
    thisString.equals(anotherString) OR thisString.equals(anotherString)
    You can replace the OR with || The compiler doesn't allow you do say is this string equal to this, that, or another. You have to make the comparison for each test you want. If you want to test if the string equals one of five different strings you will have to or 5 conditions together.

  3. #3
    Crazz is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your help :)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 07:01 PM
  2. operator - cannot be applied to java.lang.String,int
    By cc11rocks in forum AWT / Swing
    Replies: 0
    Last Post: 02-11-2011, 04:33 AM
  3. jsp insert into database error(java.lang.NumberFormatException: For input string: "")
    By cypher_girl in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-22-2009, 04:14 AM
  4. Replies: 3
    Last Post: 12-01-2008, 06:40 AM
  5. Replies: 4
    Last Post: 11-27-2008, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •