Cannot Find Symbol: Constructor

Thinking I got off track..

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2748 Views - Last Post: 06 June 2010 - 04:37 PM Rate Topic: -----

#1 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Cannot Find Symbol: Constructor

Posted 04 June 2010 - 08:29 PM

I have a Object class set up called Location. It has its 3 declarations and 3 getters and setters one for the String city, one for the double lat and one for the double lon.

Now I am trying to create my program and am #1 getting an error when I try to update my array after each pass. It is telling me: Cannot find symbol...symbol constructor Location(java.lang.String,Double,Double). Confused on that one.

Also, I am working on the equation right now to figure the distance based on lat and lon between cities, 1 by 1. Later I have to try to make it a recursive search and get the total for all 5 stopping at each one but I wanted to get it working on figuring 1-2, 2-3, 3-4...and so on first. (practice!)

So here is my code for the main part:
public class Map {
    public static void main(String[] args){
        Location[] place = new Location[5];

        boolean toContinue = true;
        String input = "";
        int menuItem = 0;
        int counter = 0;

        while(toContinue){
            input = JOptionPane.showInputDialog(null, "Please select " +
                    "from the following options:\n 1) Enter City Data\n " +
                    "2) View Report" +
                    "3) Exit");
            menuItem = Integer.parseInt(input);
            switch(menuItem){
                case 1: //getting user input
                    String city = JOptionPane.showInputDialog(null, "Enter " +
                            "city name: ");
                    double lat = Double.parseDouble(JOptionPane.showInputDialog
                            (null, "Enter Latitude of City: "));
                    double lon = Double.parseDouble(JOptionPane.showInputDialog
                            (null,"Enter Longitude of City: "));
                    place[counter++] = new Location(city, lat, lon);
                    break;
                case 2: //display results
                    int distance;
                    for(int i = 0; i < counter; ++i)
                        distance = Math.sqrt(Math.pow(place[i].lat - place
                        [i].lat,2) + place[i].lon - place[i].lon,2));
            }
        }
    }
}



p.s. I know the math equation is wrong...still trying to figure out how to feed my array into that. Also already changed distance to a double..noticed that just now.

Edited by macosxnerd101: Changed title to be more descriptive. :) "Help me," "I'm a noob," and "Java Help" are not descriptive titles in a forum dedicated to Java Help.

This post has been edited by macosxnerd101: 05 June 2010 - 08:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Find Symbol: Constructor

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Cannot Find Symbol: Constructor

Posted 04 June 2010 - 09:06 PM

And what does your class Location looks like ?

This post has been edited by pbl: 04 June 2010 - 09:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 04 June 2010 - 09:12 PM

View Postpbl, on 04 June 2010 - 08:06 PM, said:

And what does your class Location looks like ?

public class Location {
    private String city;
    private double lat, lon;
   

    public double getLat(){
        return lat;
    }
    public double getLon(){
        return lon;
    }
    public String getCity(){
        return city;
    }
    public void setLat(double lat){
        this.lat = lat;
    }
    public void setLon(double lon){
        this.lon = lon;
    }
    public void setCity(String city){
        this.city = city;
    }

}


Sorry bout that
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Cannot Find Symbol: Constructor

Posted 04 June 2010 - 09:17 PM

Your class Location does not have a constructor that expects these parameters

place[counter++] = new Location(city, lat, lon);
Was This Post Helpful? 1
  • +
  • -

#5 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 05:22 AM

View Postpbl, on 04 June 2010 - 08:17 PM, said:

Your class Location does not have a constructor that expects these parameters

place[counter++] = new Location(city, lat, lon);

Not really sure I need one since place[counter++] is just a way to continue to add array data until I have filled the array. Location is expecting city, lat, lon for its params and those are in the constructor. Evertyhing I have read thus far about OO programming suggests this would work so I must be missing something.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 05:37 AM

What pbl means is that in your Location class you should have a constructor that accepts 3 parameters.
since 'city' is of type String, 'lat' is double, and 'lon' is double, you should have a Location constructor like:
public Location(String city, double lat, double lon){
   this.city = city;
   this.lat = lat;
   this.lon = lon;
}

inside your Location class.

note that without declaring any other constructor, your Location class has the default constructor, which is actually this constructor:
public Location(){}

as you can see, this constructor accepts no parameters at all.
Was This Post Helpful? 1
  • +
  • -

#7 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 05:54 AM

Ahhh...argh sorry bout that!...I see what you are saying now though. I will get that in asap then back to work on figuring out the math portion!
Was This Post Helpful? 0
  • +
  • -

#8 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 06:11 AM

Any thoughts on using the lat and long in the math equation? Or at least where to figure out how. Basically I need it to work like the equation that is there only that one does not.
Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 06:51 AM

your lon and lat members are declared as private.
to get their value in other classes, you will have to use the public get methods you have written.
instead of:
distance = Math.sqrt(Math.pow(place[i].lat - place[i].lat,2) + place[i].lon - place[i].lon,2));

use:
distance = Math.sqrt(Math.pow(place[i].getLat() - place[i].getLat(),2) + place[i].getLon() - place[i].getLon(),2));
Was This Post Helpful? 1
  • +
  • -

#10 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 07:24 AM

View Postjapanir, on 05 June 2010 - 05:51 AM, said:

your lon and lat members are declared as private.
to get their value in other classes, you will have to use the public get methods you have written.
instead of:
distance = Math.sqrt(Math.pow(place[i].lat - place[i].lat,2) + place[i].lon - place[i].lon,2));

use:
distance = Math.sqrt(Math.pow(place[i].getLat() - place[i].getLat(),2) + place[i].getLon() - place[i].getLon(),2));


Ok so I see what you are saying and from what I have learned one wants to leave those members private, I changed some things around. Though now if I work it as shown here :
 case 2: //display results
                    double distance;
                    distance = Math.sqrt(Math.pow(place[1].getLat() - place[2].getLat(),2)
                            + Math.pow(place[1].getLon() - place[2].getLon(),2));
                    JOptionPane.showMessageDialog(null, "Distance " +
                            "from " + place[1].getCity() + " to " + 
                            place[2].getCity() + " is: " + distance + ".");

This works to display one calculation, obviously between city 1 and 2. In an effort to not have to have a ton of separate calculations I was hoping to use a counter here as well, though, when I set up the for() method, I begin to get errors because of the integer in the []. Basically I want to make it work with one equation for 1-2, 2-3, 3-4, 4-5 and then 1-5 recursively.
Was This Post Helpful? 0
  • +
  • -

#11 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 07:35 AM

Array's valid indexes are numbers between 0 and the array's length - 1.
so, in your case, I believe count is the logic size of your array.
in order to use
location[i] and location[i + 1], counter MUST be bigger than 1.
then, you have to loop from i to counter - 1, so you won't try to refer to an invalid index of your array.
so, your for loop should look like:
for(int i = 0; i < counter - 1; ++i)

of course, after making sure counter is bigger than 1.
Was This Post Helpful? 1
  • +
  • -

#12 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 09:41 AM

View Postjapanir, on 05 June 2010 - 06:35 AM, said:

Array's valid indexes are numbers between 0 and the array's length - 1.
so, in your case, I believe count is the logic size of your array.
in order to use
location[i] and location[i + 1], counter MUST be bigger than 1.
then, you have to loop from i to counter - 1, so you won't try to refer to an invalid index of your array.
so, your for loop should look like:
for(int i = 0; i < counter - 1; ++i)

of course, after making sure counter is bigger than 1.

Ok so have that working the equation works...fort of (considering I want to make it recursive to get distance from 1-5 stopping at all possible scenarios in the middele eventually) but it skips the first city entered. I have changed and rechanged the counter setting cannot seem to get it right. Also I was hoping, with this version to have the program cycle through and do 1-2, 2-3, 3-4, 4-5 all with one thing and display them all. I tried looking the output(the message dialog) but that did not work. Any thoughts or suggestions of places to read up on this stuff? API is only so helpful to me I guess.

 case 2: //display results
                    double distance = 0;
                    int temp;
                    if(counter > 0){
                         for(int i = 0; i < counter - 1; ++i)
                         distance = Math.sqrt(Math.pow(place[i+1].getLat() -
                              place[i].getLat(),2) + Math.pow(place[i+1].
                              getLon() - place[i].getLon(),2));
                         temp = (int)(distance * 100 +.5);
                         distance = temp / 100.0;
                         JOptionPane.showMessageDialog(null, "Distance from:\n" +
                                 place[0].getCity() + " to " +
                                 place[1].getCity() + " is: " + distance);
                    }                  


Editted because I found my error and why it looked like I was getting the first item in the list skipped...had it set to place[1] and [2] instead of [0] and [1]. Question though, I cannot carry the i into the display to just match up with what the calculation uses...I assume this is where the recursion would help if I knew what I was doing there?

This post has been edited by MKunstman: 05 June 2010 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#13 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 02:59 PM

I am not sure what you mean by the recursion...
you will have to write a recursive method for that, but a simple for loop would be much easier, and efficient in that case. so use a for loop and not recursive method (unless it is an assignment...).
this code:
place[0].getCity() + " to " + 
place[1].getCity() + " is: " + distance); 

is not very efficient..
suppose uou had 1000 locations. would you start hard coding:
place[0], place[1].......place[769]... place[999] ?
a better approach would be to use a for loop as I suggested before.
like so:
for(int i = 0;i < place.length - 1; i++){
    ....
    //rest of code..
    JOptionPane.showMessageDialog(null, "Distance from:\n" + 
    place[i].getCity() + " to " + 
    place[i + 1].getCity() + " is: " + distance);
}  

Was This Post Helpful? 1
  • +
  • -

#14 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 04:21 PM

View Postjapanir, on 05 June 2010 - 01:59 PM, said:

I am not sure what you mean by the recursion...
you will have to write a recursive method for that, but a simple for loop would be much easier, and efficient in that case. so use a for loop and not recursive method (unless it is an assignment...).
this code:
place[0].getCity() + " to " + 
place[1].getCity() + " is: " + distance); 

is not very efficient..
suppose uou had 1000 locations. would you start hard coding:
place[0], place[1].......place[769]... place[999] ?
a better approach would be to use a for loop as I suggested before.
like so:
for(int i = 0;i < place.length - 1; i++){
    ....
    //rest of code..
    JOptionPane.showMessageDialog(null, "Distance from:\n" + 
    place[i].getCity() + " to " + 
    place[i + 1].getCity() + " is: " + distance);
}  

Well as it stands I am allowed to do the program without the recursive function...which is what I am doing first as I fugred it would be easiest (haha). Then we can if we want to, make it with the recursive function since that is technically what we covered but our prof said it was not necessary to use it if we could not figure it out. Not sure why it is not necessary but I figured get one working format then go for the recursive version.
Was This Post Helpful? 0
  • +
  • -

#15 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: Cannot Find Symbol: Constructor

Posted 05 June 2010 - 06:30 PM

So feeling pretty snubbed at this point. Got this far...the display is not inserting the tabs I wanted...but even worse...only one calculation is taking place...though the display is now showing all 5 at once. Also still confused as to a recursive function for something like this but that is besides the point.

public class Map {
    public static void main(String[] args){
        Location[] place = new Location[5];

        boolean toContinue = true;
        String input = "";
        int menuItem = 0;
        int counter = 0;

        while(toContinue){
            input = JOptionPane.showInputDialog(null, "Please select " +
                    "from the following options:\n 1) Enter City Data\n " +
                    "2) View Report\n" +
                    "3) Exit");
            menuItem = Integer.parseInt(input);
            switch(menuItem){
                case 1: //getting user input
                    while(counter < 5){
                        String city = JOptionPane.showInputDialog
                                (null, "Enter city name: ");
                        double lat = Double.parseDouble
                                (JOptionPane.showInputDialog(null, "Enter " +
                                "Latitude of City: "));
                        double lon = Double.parseDouble
                                (JOptionPane.showInputDialog(null,"Enter " +
                                "Longitude of City: "));
                        place[counter++] = new Location(city, lat, lon);
                    }
                    break;
                case 2: //display results
                    double distance = 0;
                    int temp;
                    if(counter > 0){
                         for(int i = 0; i < counter - 1; ++i)
                         distance = Math.sqrt(Math.pow(place[i+1].getLat() -
                              place[i].getLat(),2) + Math.pow(place[i+1].
                              getLon() - place[i].getLon(),2));
                         temp = (int)(distance * 100 +.5);
                          distance = temp / 100.0;
                         
                    String display = "Distance between cities is as " +
                                 "shown.\n" + "City\tLatitude\tLongitude\t" +
                                 "Distance\n";
                    for(Location i: place)display += i.toString() + "\t" +
                            distance + "\n";
                    JOptionPane.showMessageDialog(null, display);
                    }                  
                case 3: //exit
                    System.exit(0);
            }
        }
    }
}


and the main Location object
public class Location {
    private String city;
    private double lat, lon;

    public Location(String city, double lat, double lon){
        this.city = city;
        this.lat = lat;
        this.lon = lon;
    }
   
    public double getLat(){
        return lat;
    }
    public double getLon(){
        return lon;
    }
    public String getCity(){
        return city;
    }
    public void setLat(double lat){
        this.lat = lat;
    }
    public void setLon(double lon){
        this.lon = lon;
    }
    public void setCity(String city){
        this.city = city;
    }
    public String toString(){
        return city + "\t" + lat + "\t" + lon;
    }
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2