Results 1 to 5 of 5
  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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
    Java Code:
    if(word.equals(10))
    but that only gives me if it equals 10 and i want greater than or equal too. Any help appreciated!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default 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.

  3. #3
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default 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.

  5. #5
    Dreadbolt is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Re: writing >= if statement not work with both string and int

    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.
    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.
    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

  1. Can't get Update statement to work
    By Reptar693 in forum JDBC
    Replies: 8
    Last Post: 09-01-2012, 11:36 AM
  2. Replies: 6
    Last Post: 03-31-2011, 04:33 PM
  3. Replies: 3
    Last Post: 02-23-2011, 08:25 PM
  4. SQL statement doesn't work....
    By pbaudru in forum New To Java
    Replies: 4
    Last Post: 04-08-2010, 10:51 PM
  5. Replies: 5
    Last Post: 01-11-2009, 11:12 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
  •