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

Thread: a persistent error

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy a persistent error

    please help me.

    i dont know why this keeps happening


    Program 21: Middle.java
    Write a program that will receive a person’s full name in one String object (don’t read in 3 names). Then receive a second person’s full name. Print out whether the 2 people have the same middle name or not. Do this 3 times.

    the error is: --------------------Configuration: <Default>--------------------
    incompatible types
    found : boolean
    required: java.lang.String
    String comparison= nametwo.equals(nameone);
    ^
    1 error

    Process completed.

    import java.lang.String;
    import java.util.Scanner;
    public class Middle
    {
       public static void main(String[] args) 
       {
       Scanner input=new Scanner(System.in);
       System.out.println("Please enter your full name");
       String nameone=input.nextLine();
       System.out.println ("Please enter another user's full name");
       String nametwo=input.nextLine();
     
       String comparison= nametwo.equals(nameone);
     
       if(nametwo.equals(nameone))
       {
               System.out.println (nameone   + "and" + nametwo +  "have the same middle name");
       }
       else
       {
               System.out.println (" The two names entered don't have the same middle name. ");
       }
     
      }
     
    }
    Last edited by helloworld922; November 5th, 2009 at 08:10 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: a persistent error

    The API for String (http://java.sun.com/j2se/1.5.0/docs/.../String.html):
    "public boolean equals(Object anObject)
    Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object."
    Note the return value...

  3. The Following User Says Thank You to copeg For This Useful Post:

    iank (November 5th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Re: a persistent error

    but I have one more question.. how should I correct this?

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: a persistent error

    .equals() returns a boolean, you can't put it in a string.

    boolean middleNamesMatch = nameTwo.equals(nameOne);

    However, you're already checking this in the if statement, so this is completely redundant.