*
The moose likes Beginning Java and the fly likes help, cannot find symbol - variable (using hashmaps) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help, cannot find symbol - variable (using hashmaps)" Watch "help, cannot find symbol - variable (using hashmaps)" New topic
Author

help, cannot find symbol - variable (using hashmaps)

Amy lewison
Greenhorn

Joined: Sep 10, 2008
Posts: 4
Hi, I'm having a problem trying to print the integers in the arrays stored in populateStation onto seperate lines using printAreasAndFrequencies(). It brings up the error "cannot find symbol - variable northList". Also I need to ommit the [] from the prinln. Any pointers in the right direction would be great


public class Station
{
// instance variables for class Station
private String name;
private HashMap frequenciesByArea;

//
/**
* Constructor for objects of class Station
*/
public Station(String RadioName)
{
name = RadioStationName;
frequenciesByArea = new HashMap();
}
/**
* Getter Methods for name and FrequenciesByArea
*/

public String getName()
{
return this.name;
}

public HashMap getfrequenciesByArea()
{

return this.frequenciesByArea;
}

/**
*The method for Station to populate frequencies by area
*/

public void populateStation (Integer[] north, Integer[] south,Integer[] east, Integer[] west)
{
Arrays.sort(north);
List<Integer> northList = new ArrayList<Integer>(Arrays.asList(north));


Arrays.sort(south);
List<Integer> southList = new ArrayList<Integer>(Arrays.asList(south));


Arrays.sort(east);
List<Integer> eastList = new ArrayList<Integer>(Arrays.asList(east));


Arrays.sort(west);
List<Integer> westList = new ArrayList<Integer>(Arrays.asList(west));


frequenciesByArea.put("North",northList);
frequenciesByArea.put("South",southList);
frequenciesByArea.put("East",eastList);
frequenciesByArea.put("West",westList);

}

/**
*A public instance method that print the radio name & related Areas & frequencies to the display PAne
*/

public void printAreasAndFrequencies()
{

System.out.println(name);
System.out.println("north = " + northList);
System.out.println("south = " + southList);
System.out.println("east = " + eastList);
System.out.println("west = " + westList);
}




}
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Amy,
Welcome to JavaRanch!

It brings up the error "cannot find symbol - variable northList".

northList is a local variable within the populateStation() method. This means it can only be used within that method. When printAreasAndFrequencies(), this variable is no longer in scope. You need to either make this an instance variable or pass it to the print method.

Also I need to ommit the [] from the prinln.

The [] means you are printing an array. If you want to print just the values, you can loop through the array an print out each element.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

Indeed, the northList reference definitely went out of scope. However, the object that it was originally pointing to was placed in the frequenciesByArea map first. This means that you can retrieve the "northList" object via a get from the map.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Amy lewison
Greenhorn

Joined: Sep 10, 2008
Posts: 4
Thanks for the headsup, I'm trying to retrieve the "northList" object via a get from the map using:

public static void printAreasAndFrequencies()
{


System.out.println("north =" + HashMap.getfrequenciesByArea(northList));
System.out.println("south = " );
System.out.println("east = ");
System.out.println("west = ");
}

But I'm gettin an error message of "cannot find variable - symbol northList". I know it's probably something really simple... I've been looking at this for hours... aaarrrrhhh..

any help?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Your printAreasAndFrequencies method is never going to work in its present state.
How on earth do you get a method called getFrequenciesByArea out of the HashMap class? Go back to your getFrequenciesByArea method and see what it actually does.
Why have you got the frequenciesByArea Map as an instance variable? You only need one copy of it. How are you populating that Map? How are you putting the Stations into their respective Lists? Are you repeatedly putting new Lists into the Map?
Amy lewison
Greenhorn

Joined: Sep 10, 2008
Posts: 4
Hi, by this statement

"How on earth do you get a method called getFrequenciesByArea out of the HashMap class?"

Do you mean that it is not possible to do this? I will be intending on putting more lists into the map.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

I agreed with Campbell. You were able to get it into the map. You should be able to get it out of the map, in a similiar fashion.

From your new code. it doesn't look like you even understand hashmaps at all. How were you able to instantiate and put the objects into the hashmap in the first place? Heck, you even define keys for each list.

Henry
Amy lewison
Greenhorn

Joined: Sep 10, 2008
Posts: 4
Sorry I'm having a moment... Something along the lines of:

public void printAreasAndFrequencies()
{

frequenciesByArea.get("North");
System.out.println("north = " + frequenciesByArea.get("North"));

frequenciesByArea.get("South");
System.out.println("south = " + frequenciesByArea.get("South"));

frequenciesByArea.get("East");
System.out.println("east = " + frequenciesByArea.get("East"));

frequenciesByArea.get("West");
System.out.println("west = " + frequenciesByArea.get("West"));
}

Should be OK

Sorry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

Sorry I'm having a moment... Something along the lines of:


Not a problem. We all have those "moments"...

BTW, you can get rid of the standalone calls from the printAreasAndFrequencies() method...



I am pretty sure that you place them there to test the syntax. All they do is get the object from the hashmap, and thow it away. The System.out.print calls fetches it directly from the hashmap again.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Originally posted by Amy lewison:
Hi, by this statement

"How on earth do you get a method called getFrequenciesByArea out of the HashMap class?"

Do you mean that it is not possible to do this?


You were invoking a method called getFrequenciesByArea on "HashMap." which suggests it is a static method of the HashMap class.
 
 
subject: help, cannot find symbol - variable (using hashmaps)
 
Similar Threads
enum (310-065)
overriden method
Enum Problem
few scenarios in JAVA where instinct doesn'n play right
What am I doing wrong?