Results 1 to 5 of 5
- 04-19-2012, 11:20 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
writing >= if statement not work with both string and int
I want to say, if the word is greater than or equal to 10, .. do such and such,
but i know that :
if(word >= 10) // word being the String
// something here
else
// something here
I know that is because its "undefined for the arguments string and int.
Alternatively i tried
but that only gives me if it equals 10 and i want greater than or equal too. Any help appreciated!Java Code:if(word.equals(10))
- 04-19-2012, 11:28 PM #2
Re: writing >= if statement not work with both string and int
What data type is the variable: word? If it's a String, use the Integer class's parse method to convert it to an int value that you can compare with < or >=
Also the String class has methods for comparing the contents of Strings.If you don't understand my response, don't ignore it, ask a question.
- 04-19-2012, 11:44 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: writing >= if statement not work with both string and int
yes word is a string.
And as for the second part,
I noticed that its possible to use this to compare strings:
(String1.compareTo(String2) < 0)
But in my case im not trying to compare two strings im trying to compare one string with a number, as in, if the length of the word is 10 or greater numbers.
- 04-19-2012, 11:46 PM #4
Re: writing >= if statement not work with both string and int
Make the number a String or make the String a number.
The length of a String is something else.If you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 03:17 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 1
- Rep Power
- 0
Re: writing >= if statement not work with both string and int
You cannot compare a String and an int the way you're trying to. As you are trying to compare the length of a string with a number, you should use the String.length() method.But in my case im not trying to compare two strings im trying to compare one string with a number, as in, if the length of the word is 10 or greater numbers.
Try something like this:
if (yourString.length() >= yourNumber) {
// length of string is at least as high as said number. do whatever
} else {
// length of string is less than number. do whatever
}
Similar Threads
-
Can't get Update statement to work
By Reptar693 in forum JDBCReplies: 8Last Post: 09-01-2012, 10:36 AM -
Can't figure out why my Else statement won't work
By basla in forum New To JavaReplies: 6Last Post: 03-31-2011, 03:33 PM -
Excel - (workbook.write();) in IF statement will not work.
By Daedalus357 in forum New To JavaReplies: 3Last Post: 02-23-2011, 07:25 PM -
SQL statement doesn't work....
By pbaudru in forum New To JavaReplies: 4Last Post: 04-08-2010, 09:51 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks