Results 1 to 4 of 4
- 05-02-2011, 07:05 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
[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,
CrazzLast edited by Crazz; 05-02-2011 at 07:26 AM.
- 05-02-2011, 07:18 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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)
- 05-02-2011, 07:20 AM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Thanks for your help :)
- 05-02-2011, 07:51 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
You are welcome, glad to have helped you!
Similar Threads
-
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
operator - cannot be applied to java.lang.String,int
By cc11rocks in forum AWT / SwingReplies: 0Last Post: 02-11-2011, 03:33 AM -
jsp insert into database error(java.lang.NumberFormatException: For input string: "")
By cypher_girl in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-22-2009, 03:14 AM -
[SOLVED] operator / cannot to applied to java.lang.string,int
By tpyq in forum NetBeansReplies: 3Last Post: 12-01-2008, 05:40 AM -
Error Message: operator * cannot be applied to java.lang.String, int
By MICHAELABICK in forum Java AppletsReplies: 4Last Post: 11-27-2008, 06:09 AM
Bookmarks