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 3 of 3

Thread: Wasn't this supposed to work?

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Wasn't this supposed to work?

    public class test {

    public static void main(String[] args) {
    int x=1000;

    if (Integer.toString(x).length.equals(4) )
    System.out.println("Hi");

    }
    }

    How come this doesn't work?

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    92
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Wasn't this supposed to work?

    1.) 'length' is a function call on the String object, so it needs parenthesis.

    2.) The String 'length' function returns a primitive int, not an object, so you'll need to use '==' instead of 'equals()'.

    3.) Please be sure and use code tags to get highlighting and preserve formatting:

    [C0DE]
    **YOUR CODE GOES HERE**
    [/C0DE]

    (Use O, not zero).

    Hope that helps!

  3. #3
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Wasn't this supposed to work?

    If you want to compare String, .equals() is the choice.
    If you are comparing int, use == instead.
    Whatever you are, be a good one

Similar Threads

  1. Isn't the result supposed to be 6?
    By obel1x in forum Java Theory & Questions
    Replies: 3
    Last Post: April 12th, 2014, 03:43 AM
  2. Can anybody help me fix this, what I am supposed to do is at the top
    By peplo1214 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2012, 07:05 AM
  3. Replies: 6
    Last Post: November 25th, 2011, 03:58 PM
  4. Not sure what I'm supposed to do...
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2011, 11:13 PM
  5. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM