4 Replies - 305 Views - Last Post: 22 March 2010 - 04:00 PM Rate Topic: -----

#1 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Why is it saying that my variables weren't declared?

Posted 22 March 2010 - 03:34 PM

When I try to compile the following I get
the error: java:51: variable latitude might not have been initialized
for all of the variables that are declared lines 11 through 19. I have no idea why this is. All of the dependencies compile correctly. Any ideas? Thank you.
import javafoundations.*;
import java.util.*;
import java.net.*;
import java.io.*;

public class Activity1 {
	public static void main(String[] args) throws MalformedURLException, IOException {
		URL url= new URL("http://depasquale-1.tcnj.edu/~depasqua/csc230/dataSources/zipcodes.txt");
		Scanner sc= new Scanner(url.openStream());
		ArraySet<Zipcode> zipcodes= new ArraySet<Zipcode>();
		Zipcode zc;
		String zip;
		String latitude;
		String longitude;
		String city;
		String state;
		String county;
		String type;
		while(sc.hasNextLine()) {
			Scanner sc2 = new Scanner(sc.nextLine()).useDelimiter(",");
			if(sc2.hasNext()) {
				zip=sc2.next();
				if(sc2.hasNext()) {
					latitude=sc2.next();
					if(sc2.hasNext()) {
						longitude=sc2.next();
						if(sc2.hasNext()) {
							city=sc2.next();
							if(sc2.hasNext()) {
								state=sc2.next();
								if(sc2.hasNext()) {
									county=sc2.next();
									if(sc2.hasNext()) {
										type=sc2.next();
									}
								}
							}
						}
					}
				}
				if(!zip.equals("")&&!latitude.equals("")&&!longitude.equals("")&&!city.equals("")&&!state.equals("")&&!county.equals("")&&!type.equals("")) {
					zc=new Zipcode(zip, latitude, longitude, city, state, county, type);
					zipcodes.add(zc);
				}
			}
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why is it saying that my variables weren't declared?

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Why is it saying that my variables weren't declared?

Posted 22 March 2010 - 03:45 PM

Because you do not give latitude (or the others) an initial value. If one of the if statements fail, then your program will be left with a variable without a value.

Sometimes Java is overzealous with this warning, and will issue the error even if you can see that the ifs will always evaluate to true, but it's helpful annoyance.
Was This Post Helpful? 1
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Why is it saying that my variables weren't declared?

Posted 22 March 2010 - 03:47 PM

As a rule of thumb, I always initialize my variables, then just redefine them later. So instead of:
String zip;
                String latitude;
                String longitude;
                String city;
                String state;
                String county;
                String type;


I would do this:
String zip;
                String latitude = "";
                String longitude = "";
                String city = "";
                String state = "";
                String county = "";
                String type = "";



I think it has something to with making space in memory for the object and there not being anything to reference when you call it.
Was This Post Helpful? 2
  • +
  • -

#4 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: Why is it saying that my variables weren't declared?

Posted 22 March 2010 - 03:56 PM

View Postzim1985, on 22 March 2010 - 04:47 PM, said:

As a rule of thumb, I always initialize my variables, then just redefine them later. So instead of:
String zip;
                String latitude;
                String longitude;
                String city;
                String state;
                String county;
                String type;


I would do this:
String zip;
                String latitude = "";
                String longitude = "";
                String city = "";
                String state = "";
                String county = "";
                String type = "";



I think it has something to with making space in memory for the object and there not being anything to reference when you call it.



Got it. Very useful. Thanks to both of you.
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Why is it saying that my variables weren't declared?

Posted 22 March 2010 - 04:00 PM

a global String variable is initilized "automatically" by the compiler to null.
a local String variable does not.
try running that simple code:

public class TestClass {

//static so can be used at the main
static String globalString;

public static void main(String[] args){
    String localString;
    
    //global String is initilized by the compiler to null, so this will prints null:
    System.out.println(globalString);

    //trying to print the localString will give you an error, as a local variable gets no reference at all. 
    //System.out.println(localString);
}
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1