Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java compiling issues

    I'm sure this is really simple, but as I've just started coding Java, really, I can't figure out why this is happening. First, the code.

    BusinessObject.java
    PHP Code:
    public abstract class BusinessObject {
        public 
    Vector validationMessage;
        protected 
    HashMap data = new HashMap();
        public 
    HashMap read () {
           return 
    data;
        }

    Person.java
    PHP Code:
    public abstract class Person extends BusinessObject {
        public 
    Person() {
        }
        public 
    Person (String nameString) {
            
    String newLast "";
            
    String newFirst "";
            if ( 
    nameString.indexOf(",") != -) {
                
    newLast nameString.substring0nameString.indexOf(",") );
                
    newFirst nameString.substringnameString.indexOf(",") + 1nameString.length() );
            } else if ( 
    nameString.indexOf(" " ) != -) {
                
    newFirst nameString.substring0nameString.indexOf(" ") );
                
    newLast nameString.substringnameString.indexOf(" ") + 1nameString.length() );
            }
            
    Person(newLastnewFirst);
        }
        public 
    Person (String newNameLastString 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.

    What am I doing wrong here?

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Change the line to:
    Code:
    this(newLast, newFirst);
    In cases you'll want to call the superclass's constructor it's super(...).

    shmoove

    Edit:
    The super(...) comment is irrelevant in your case, just for future reference.

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried this(newLast, newFirst) before I posted, actually, and a different compiler error happens: "call to this must be first statement in constructor."

    I've thought of replicating the actual data.put() calls into the Person(nameString) constructor, but at that point my understanding of the flexibility and convenience of overloaded methods breaks down sobbing and runs away into its stash of Xanax.

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    (double-posting, not editing, because of the time delay and the content)

    I've found a solution, but it's not one I like, because it's not very legible,friendly, or efficient. Anybody got anything better?

    PHP Code:
    public Person (String nameString) {
        
    this(
            
    nameString.indexOf(",") != -
                
    nameString.substring0nameString.indexOf(",") ) :
                
    nameString.substring(nameString.indexOf(" ") + 1nameString.length() )
            ,
            
    nameString.indexOf(",") != -
                
    nameString.substring(nameString.indexOf(", ") + 1nameString.length() ):
                
    nameString.substring0nameString.indexOf(" ") )
        );

    I don't like using the same ternary operation twice in the code. I don't like stretching argument definitions out like that. I don't like being so damned confusing that even clear indentation doesn't help the legibility.

    Grr.

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or you could do this:
    Code:
    private void construct(String newNameLast, String newNameFirst) { 
      data.put("nameLast", newNameLast); 
      data.put("nameFirst", newNameFirst); 
    }
    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() ); 
      } 
      construct(newLast, newFirst); 
    }
    public Person (String newNameLast, String newNameFirst) {
      construct(newNameLast,newNameFirst);
    }
    shmoove

  • #6
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does that follow any recommended practices? It looks decent, but it seems to me that having a non-constructor method do the work of a constructor method would be frowned-upon. True, it works. True, I don't know a lot yet. True, I'm picking nits.

    Thanks for the help.

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know about any recommended practices, just going by common sense.
    But I think that by making that method private you couldn't be violating too many recomendations. It's just a helper function that has some reusable code. There's no rule (or recommendation) against calling methods from the constuctor as far as I know. And since it's private it has no effect on the interfaces you expose, so it's effect on the architecture of the application is non-existant.

    shmoove

    Edit:
    PS: If it helps you sleep better you can rename the method to something other than "construct". Call it "createData" or something. ;P
    Last edited by shmoove; 04-07-2004 at 12:57 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
    •