Question

public class Location {
private String city;
private double latitude;
private double longitude;

public Location(String aCity, double aLatitude, double aLongitude)
{
    city = aCity;
    latitude = aLatitude;
    longitude = aLongitude;
}
void setLocation(String theCity)
{
    city = theCity.trim();
}
void setLatitude(double lat)
{
    latitude = lat;
}
void setLongitude(double long1)
{
    longitude = long1;
}
public String getLocation()
{
    return city;
}
public double getLatitude()
{
    return latitude;
}
public double getLongitude()
{
return longitude;
}
public String tooString()
{
    String result = String.format("City: %s (%1.3f; %1.3f)", city, latitude,longitude);
    return result;
}

Main program:

public class Hmwk {

public static void main(String[] args) throws FileNotFoundException {
    Scanner input=new Scanner (new File ("input.txt"));
    while (input.hasNextLine())
    {
        String line=input.nextLine();
        String[] tokens;
        tokens = line.split("\t");
        String city=tokens[0];
        double lat=Double.parseDouble(tokens[1]);
        double longy=Double.parseDouble(tokens[2]); //Error
        Location loc=new Location(city,lat,longy);
        loc.tooString();

    }


}

I'm getting an ArrayIndexOutOfBoundsException noted in the main program which I don't quite understand.

Input is divided by tabs even if it doesn't look like it...

St. Joseph, MO  +39.76580   -94.85060
Shanghai,China  +31.23300   +121.45000
Kansas_City,KS     +39.11780    -94.64000
Was it helpful?

Solution 4

Your String is:

St. Joseph, MO  +39.76580   -94.85060

You're splitting it on the tab locations, which there are none in that line. Your array, therefore is of length 1.

tokens[0] = St. Joseph, MO  +39.76580   -94.85060
tokens[1] // doesn't exist.

Additionally the input you've provided is also not formatted in a tabular way:

St. Joseph, MO  +39.76580   -94.85060
Shanghai,China  +31.23300   +121.45000
Kansas_City,KS     +39.11780    -94.64000

Notice the last line isn't properly lined up with the others? They would be if you were using hard tabs. Make sure you're not using spaces in lieu of tabs but are actually using the tab button (or equivalent character if data is formatted programmatically).

I recreated the entire project from your own code and got the same error you did. I deleted the spaces and replaced them with actual Tab characters and it works fine.

EDIT After the data works it doesn't print

This is a simple fix. You're calling the toString() method but that does you no good when you don't do anything with the String value returned to you.

Just make it:

System.out.println(loc.tooString());

OTHER TIPS

This is a working example, you problem comes from the input.txt file. If one of the Tab character (\t) is missing (mistyped Space instead) then split() will return less elements then 3.

1- Make a test to check if there are actually 3 elements:

if (tokens.length >= 3) {
 String city=tokens[0];
 double lat=Double.parseDouble(tokens[1]);
 ...
}

2- Check your input.txt file for spaces or missing Tab characters.

3- You should use toString() in your class instead of tooString(). This method is meant for this purpose :)

Working Class:

public class Hmwk {
 public static void main(String[] args) throws FileNotFoundException {
    Scanner input=new Scanner (new File ("input.txt"));
    while (input.hasNextLine())
    {
        String line=input.nextLine();
        String[] tokens = line.split("\t");
        if (tokens.length >= 3) {
          String city=tokens[0];
          double lat=Double.parseDouble(tokens[1]);
          double longy=Double.parseDouble(tokens[2]);
          Location loc=new Location(city,lat,longy);
          System.out.println(loc.toString());
       }
    }
    input.close();
}

}

In the code below you are assuming that line you read from file has 3 tokens separated by '\t' you will get a ArrayIndexOutOfBound exception when there are not enough tokens. You should check the size of token array before you run the code

tokens = line.split("\t");
if (tokens.length == 3){
    String city=tokens[0];
    double lat=Double.parseDouble(tokens[1]);
    double longy=Double.parseDouble(tokens[2]);
}

When you declared the array, tokens[], you did not specify a size for it.

    String[] tokens;
    tokens = line.split("\t");
    String city=tokens[0];

So when you go and set longy to be tokens[2], the 2 spot does not exist.

you should change the code to specify a size:

    String[] tokens = new String[*size*];
    tokens = line.split("\t");
    String city=tokens[0];

Cheers

Do you have any blank lines in your file? In that case I think it may be possible that it returns a zero length array, in which case the 0th element will not exist. The documentation for split leads to here: http://developer.android.com/reference/java/util/regex/Pattern.html#split(java.lang.CharSequence, int)

To protect against that it's a good idea to wrap it in a check for the number of elements returned from the split:

if (tokens.length >= 3) {
    String city=tokens[0];
    double lat=Double.parseDouble(tokens[1]);
    double longy=Double.parseDouble(tokens[2]); //Error
    Location loc=new Location(city,lat,longy);
    loc.tooString();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top