Results 1 to 14 of 14
  1. #1
    lodaSchitt is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default found java.lang string expected person

    I'm inputting a value eg "Jarse,Hugh" as a value for Person and I get the error above.
    Here is the definition for person:

    public class Person
    {
    // The name of this person.
    private final String name;

    /**
    * Create a new person with the given name.
    * @param name The person's name.
    */
    public Person(String name)
    {
    this.name = name;
    }

    /**
    * @return The person's name.
    */
    public String getName()
    {
    return name;
    }
    }

    Error msg: "Error: Incompatible types - Found java.lang.String but expected Person"
    The code that throws the error is below:

    <code>
    public void bidFor(int lotNumber, Person bidder, long value)
    {
    Lot selectedLot = getLot(lotNumber);
    if(selectedLot != null) {
    boolean successful = selectedLot.bidFor(new Bid(bidder, value));
    if(successful) {
    System.out.println("The bid for lot number " +
    lotNumber + " was successful.");
    }
    else {
    // Report which bid is higher.
    Bid highestBid = selectedLot.getHighestBid();
    System.out.println("Lot number: " + lotNumber +
    " already has a bid of: " +
    highestBid.getValue());
    }
    }}
    </code>
    Last edited by lodaSchitt; 04-28-2011 at 01:50 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Where is the code that throws the error? Include it and the exact error, copy/paste it. Also, please use code tags.

    [code ]<-omit space
    YOUR CODE HERE
    [/code]

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    There's nothing obviously wrong with the Person class (although it's more of a Name class ;)). It looks like you're trying to assign a String to a Person reference.

    Please post the full error message text and the code where you're using Person.

  4. #4
    lodaSchitt is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Additional details

    "Error: Incompatible types - Found java.lang.String but expected Person"
    This error is received when I try to enter a value for Person.
    I think I need to use AnyRef but dont know where to put it

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You still haven't showed us the code that generated the error. Did you do something like this

    Java Code:
    Person p = "bob"
    If so, this is incorrect, you should be using the new keyword and the constructor like this
    Java Code:
    Person p = new Person("bob");

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    What is 'AnyRef' and why do you think you need it?

  7. #7
    lodaSchitt is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Here is the code that threw the error:
    Error msg: "Error: Incompatible types - Found java.lang.String but expected Person"
    The code that throws the error is below:

    <code>
    public void bidFor(int lotNumber, Person bidder, long value)
    {
    Lot selectedLot = getLot(lotNumber);
    if(selectedLot != null) {
    boolean successful = selectedLot.bidFor(new Bid(bidder, value));
    if(successful) {
    System.out.println("The bid for lot number " +
    lotNumber + " was successful.");
    }
    else {
    // Report which bid is higher.
    Bid highestBid = selectedLot.getHighestBid();
    System.out.println("Lot number: " + lotNumber +
    " already has a bid of: " +
    highestBid.getValue());
    }
    }}
    public Bid(Person bidder, long value)
    {
    this.bidder = bidder;
    this.value = value;
    }


    </code>
    Last edited by lodaSchitt; 04-28-2011 at 02:07 AM.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    @op: a good rule of thumb is to act like we know nothing about the problem. The more detail you provide, the easier it is for us to help you. Also, people can be lazy, more detail helps you work through it(possibly finding the answer) and helps us understand the problem with less effort.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, the person you are passing into that method, are you passing in an actual person object, or just the persons name as a string?

  10. #10
    lodaSchitt is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Person's name as a string. I dont know how to pass in a person object

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Using the new keyword and the constructor, you can either create the object and then lass it to the method
    Java Code:
    Person p = new Person("bob");
    and then pass p into the method, or if you don't need to refer to the person outside of the method you can simply pass
    Java Code:
    new Person("bob");
    to the method.

  12. #12
    lodaSchitt is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default wont work

    I'm inputting the value thru a gui ie a text box

  13. #13
    lodaSchitt is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default found a workaround

    I passed it in as a string and then assigned it to a new person object and now it works. Thankyou so much!!

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome. Glad you solved it, please mark your thread solved with the thread tools at the top of the page.

Similar Threads

  1. Array Required, but java.lang.String found
    By raider00321 in forum New To Java
    Replies: 5
    Last Post: 11-28-2010, 03:06 PM
  2. Replies: 7
    Last Post: 11-03-2009, 03:18 AM
  3. Replies: 2
    Last Post: 10-30-2009, 12:55 PM
  4. Replies: 3
    Last Post: 05-21-2009, 04:48 PM
  5. Replies: 2
    Last Post: 09-18-2008, 08:30 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
  •