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:
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);
}
}
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:
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
}
}
Re: Trouble with a Map Method
Quote:
Originally Posted by
Fubarable
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:
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
Re: Trouble with a Map Method
Quote:
Originally Posted by
javadum
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!