Results 1 to 3 of 3
  1. #1
    baltimore is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String)

    Hi, I'm sure this is really simple, I can't figure out why this is happening. First, the code.

    Java Code:
    public abstract class BusinessObject {
        public Vector validationMessage;
        protected HashMap data = new HashMap();
        public HashMap read () {
           return data;
        }
    }
    Java Code:
    public abstract class Person extends BusinessObject {
        public Person() {
        }
        public Person (String nameString) {
            String newLast = "";
            String newFirst = "";
            if ( nameString.indexOf(",") != -1 ) {
                newLast = nameString.substring( 0, nameString.indexOf(",") );
                newFirst = nameString.substring( nameString.indexOf(",") + 1, nameString.length() );
            } else if ( nameString.indexOf(" " ) != -1 ) {
                newFirst = nameString.substring( 0, nameString.indexOf(" ") );
                newLast = nameString.substring( nameString.indexOf(" ") + 1, nameString.length() );
            }
            Person(newLast, newFirst);
        }
        public Person (String newNameLast, String newNameFirst) {
            data.put("nameLast", newNameLast);
            data.put("nameFirst", newNameFirst);
        }
    }
    When I try to compile Person.java, the compiler tells me that at the line Person(newLast, newFirst); inside the Person (String nameString) constructor, it 'cannot resolve symbol' on Person (java.lang.String, java.lang.String). I've moved the last Person constructor above the others, between the others, etc., but I know that shouldn't make a difference.
    I've tried casting newLast = (String) nameString.substring(... and that doesn't work.
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    The way to call another constructor is with the "this" keyword. And it must be the first line in the constructor. I don't see any clean way of doing it here. You'll have to add the two strings to the map in this single–argument constructor.
    Java Code:
    public Person (String nameString) {
        this(string1, string2);
        ...
    }
    I came up with this but it's a little bizarre. Had to get rid of the abstract modifier to use it. Compiles and runs.
    Java Code:
    import java.util.*;
    
    public class PersonTest {
        public static void main(String[] args) {
            Person john = new Person("Lucas,John");
            System.out.println("john = " + john);
        }
    }
    
    class Person extends BusinessObject {
        public Person() {
        }
    
        public Person (String newNameLast, String newNameFirst) {
            data.put("nameLast", newNameLast);
            data.put("nameFirst", newNameFirst);
        }
    
        public Person (String nameString) {
            this(Person.breakUp(nameString));
        }
    
        private Person(String[] names) {
            this(names[0], names[1]);
        }
    
        private static String[] breakUp(String nameString) {
            String newLast = "";
            String newFirst = "";
            if ( nameString.indexOf(",") != -1 ) {
                newLast = nameString.substring( 0, nameString.indexOf(",") );
                newFirst = nameString.substring( nameString.indexOf(",") + 1,
                                                 nameString.length() );
            } else if ( nameString.indexOf(" " ) != -1 ) {
                newFirst = nameString.substring( 0, nameString.indexOf(" ") );
                newLast = nameString.substring( nameString.indexOf(" ") + 1,
                                                nameString.length() );
            }
            return new String[] { newLast, newFirst };
        }
    }

  3. #3
    dhnsekaran is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 01-13-2008, 11:36 PM
  2. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM
  3. Replies: 1
    Last Post: 08-02-2007, 04:07 PM
  4. Replies: 1
    Last Post: 07-31-2007, 11:47 PM
  5. Can't convert java.lang.String to int.
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-13-2007, 05:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •