Results 1 to 4 of 4
  1. #1
    javadum is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Trouble with a Map Method

    Hi all,

    sorry about my dumminess but I keep on having a ')' expected error from the compiler and I don't undersand why.

    The aim is to write a method (addLibrary) that add to the map object libraries a key name and a value library.
    Can you help me? This is the code:

    Java Code:
    import java.util.*;
    
    public class libraryBook
    {
       /* instance variables */
       /**
        * 
        */
       private String Libraries; 
       /**
        * Default constructor for objects of class libraryBook modified to create a Map interface
        * and assign it to the variable Libraries
        */
       public libraryBook()
       {
          super();
          Map<String, String> Libraries = new HashMap<String, String>();
       }
    
    
       /* instance methods */
    
       /**
        * A method to add names and Libraries to the collection Libraries 
        * The method alerts if Libraries are updated
        */
       public void addLibrary(String aName, String alibrary)
       {
          Libraries.put(String aName, String alibrary);
       }
    }
    Last edited by Fubarable; 07-15-2012 at 07:11 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble with a Map Method

    Suggestions:
    • First of all, you will want to learn Java naming conventions including that variable and method names should start with a lower case letter while classes with an upper case letter. This may seem trivial to folks starting out, but can be very important especially when asking folks not familiar with your code to try to understand it and try to help you out.
    • Next note that you do declare a Libraries variable in the class, but what type is it (String!)? What type should it be?
    • And you do initialize a Libraries variable in your constructor, but it's not the same variable as the one in the class. For one it's a different type from the one in the class, but more importantly, you re-declare the variable in the constructor making it visible *only* in the constructor.
    • To solve this, change the type of the variable declared at the top of your class.
    • Don't re-declare the variable in the constructor, just use the one already declared in the class and construct it.
    • Change your variable's name to conform with convention.
    • Next we'll talk about using [code] [/code] tags when posting code in this forum. ;-)


    Note for illustrative purposes:
    Java Code:
    public class MyClass {
       private int foo;  // here I declare the foo variable.
       private int bar = 3;  // here I declare and initialize bar;
    
      public MyClass() {
         foo = 7; // here I initialize the foo variable.
    
         // here I declare a totally new bar variable and initialize it.
         int bar = 5;  // this has no effect on the bar declared in the class
      }
    }
    Last edited by Fubarable; 07-15-2012 at 07:19 PM.

  3. #3
    javadum is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Trouble with a Map Method

    Quote Originally Posted by Fubarable View Post
    Suggestions:
    • First of all, you will want to learn Java naming conventions including that variable and method names should start with a lower case letter while classes with an upper case letter. This may seem trivial to folks starting out, but can be very important especially when asking folks not familiar with your code to try to understand it and try to help you out.
    • Next note that you do declare a Libraries variable in the class, but what type is it (String!)? What type should it be?
    • And you do initialize a Libraries variable in your constructor, but it's not the same variable as the one in the class. For one it's a different type from the one in the class, but more importantly, you re-declare the variable in the constructor making it visible *only* in the constructor.
    • To solve this, change the type of the variable declared at the top of your class.
    • Don't re-declare the variable in the constructor, just use the one already declared in the class and construct it.
    • Change your variable's name to conform with convention.
    • Next we'll talk about using [code] [/code] tags when posting code in this forum. ;-)


    Note for illustrative purposes:
    Java Code:
    public class MyClass {
       private int foo;  // here I declare the foo variable.
       private int bar = 3;  // here I declare and initialize bar;
    
      public MyClass() {
         foo = 7; // here I initialize the foo variable.
    
         // here I declare a totally new bar variable and initialize it.
         int bar = 5;  // this has no effect on the bar declared in the class
      }
    }
    Ok, I'll try to check everything you said.
    Thank you very much and sorry for not having used the appropriate tags.

    javadum

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble with a Map Method

    Quote Originally Posted by javadum View Post
    Ok, I'll try to check everything you said.
    Thank you very much and sorry for not having used the appropriate tags.
    Good luck! Please let us know how it works out!

Similar Threads

  1. Trouble with calling a method
    By jrJava in forum New To Java
    Replies: 6
    Last Post: 01-17-2011, 09:51 PM
  2. trouble with array method
    By kpro862 in forum New To Java
    Replies: 6
    Last Post: 05-12-2010, 12:08 AM
  3. [SOLVED] Method trouble &gt;:0!
    By PureAwesomeness in forum New To Java
    Replies: 10
    Last Post: 02-20-2009, 06:18 AM
  4. Trouble with method
    By BlueJ2008 in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 09:05 PM
  5. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 10:16 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
  •