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

Thread: Syntax Question...

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Syntax Question...

    Good evening all,

    I hope everyone is well. I'm OK, but I need a little help trying to figure out how to properly code a particular statement, namely this:

             if (input.indexOf("a").equals("a"))

    Obviously that is not correct, .equals is not going to work here, but I also know that I cannot use the == operator either. I can't say:
    if(input.indexOf("a") == "a")
    I'm working on an Atbash cipher, i.e. I need to find a character in a string and swap it with another. In this case, "a" will become "x" and so on.

    If anyone can help, I would really appreciate it. Thanks guys.


  2. #2
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Syntax Question...

    Hi,

    indexOf() method returns only a index number it not return letter or string. So dont compare using equals() method.

    Generally if you want to compare number means you can use == operator. If you want to compare string means you can use equals() method

    k. If you want to swap letter means you can use replace(oldChar, newChar)

    Happy to help

    Thanks,

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Syntax Question...

    Ahhhhhhh. Well that would make things much simpler indeed. Mucho appreciado Ganeprog. And thanks for explaining things as well.

  4. #4
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Syntax Question...

    Thanks, Happy coding

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Syntax Question...


  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Syntax Question...

    I'm going to say what Ganeprog said differently:

    The equals() method is used to determine whether one object is equivalent to another object, as in:

    this.equals( that );

    where 'this' and 'that' are two objects. equals() will return true if 'this' and 'that' are of the same type and have the same attributes, false otherwise. The Object class includes an equals() method, and since all Java classes descend from Object, all Java classes have an equals() method. However, the equals() method provided by the Object class may not give the desired results for every Java class, so it is the programmer's responsibility to override Object.equals() as needed. Writing a good and proper equals() method is a good study topic if you get bored and need something to do.

    Primitives (int, double, char, etc.) are not objects so cannot use a method following the dot, '.', operator like .equals() as objects can. We use and treat Strings as primitives so often that we forget they are objects, and we're surprised and disappointed when a String object rudely reminds us of that fact by not "acting" as we'd hoped it would.

    Keep coding.

Similar Threads

  1. if statement syntax question
    By lanmonster in forum Java Theory & Questions
    Replies: 2
    Last Post: January 27th, 2013, 03:29 PM
  2. Syntax of main
    By RKM904 in forum The Cafe
    Replies: 2
    Last Post: September 7th, 2012, 01:09 PM
  3. Syntax in method
    By tarkal in forum Java Theory & Questions
    Replies: 2
    Last Post: September 23rd, 2011, 03:01 PM
  4. Lazy Thursday Question: Weird Syntax You've Seen?
    By KevinWorkman in forum The Cafe
    Replies: 14
    Last Post: May 11th, 2011, 04:00 AM
  5. syntax question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2011, 04:01 PM