Question: I keep on getting an IOException in my program.java file when receiving a filename from TestProgram.java file.? Can anyone show me why? The testProgram.java file

I keep on getting an IOException in my program.java file when receiving a filename from TestProgram.java file.? Can anyone show me why?

The testProgram.java file is a exercise that can't be changed, I must adjust the program.java file to be able to process data from the called files.

Thanks for your time and effort!

import java.io.*;

public class Program {

private int cityCount; private City cityArray[]; private CompressedArray array;

private String buffer = null; private BufferedReader in = null; /* Open the file whose name was give as parameter */ public Program(String fileName, boolean showMap) { try { in = new BufferedReader(new FileReader(fileName)); buffer = in.readLine(); // Store first line } catch (IOException e) { System.out.println("Cannot read file \'"+fileName+"\'"); System.exit(0); } } /* Returns true if the end of the file has been reached; it returns false otherwise. */ public boolean endOfFile() { if (buffer == null) return true; else return false; } /* Reads a line from the file. */ public String readString() { String line = buffer; if (buffer == null) { System.out.println("Error. The end of file was reached, so another string cannot be read from it"); return null; } try { buffer = in.readLine(); } catch (IOException e) { System.out.println("Error. Cannot read from file"); System.exit(0); } return line; } /* Reads an integer from the file. Returns -1 if no integer could be read. */ public int readInt() { String line = readString(); if (line == null) return -1; else return Integer.parseInt(line); } /* Reads an double from the file. Returns -1.0 if no integer could be read. */ public double readDouble() { String line = readString(); if (line == null) return -1.0; else return Double.parseDouble(line); }

public City[] getCityArray() { return cityArray; } public void expandCapacity() { City temp[]=cityArray; City cityArray[]=new City[temp.length+3]; for(int i=0;i cityArray[i]=temp[i]; } public double distBetweenCities(City x,City y) { double dis=Math.sqrt((y.getY() - x.getY()) * (y.getY() - x.getY()) + (y.getX() - x.getX()) * (y.getX() - x.getX())); return dis; } public void compareDistances() { double arr[][]=new double[cityArray.length][cityArray.length];

for(int i=0;i { for(int j=0;j { arr[i][j]=distBetweenCities(cityArray[i],cityArray[j]); } } } public CompressedArray getArray() { return array; } }

public class TestProgram {

public static void main(String[] args) { String[] files = new String[] { "cities1.txt", "cities2.txt", "cities3.txt", "cities4.txt", "cities5.txt" }; int[] sizes = new int [] { 21, 15, 10, 15, 36 }; String[] solns = new String[] { " 36.88 441.59 441.44 420.49 421.01 22.20 396.09 399.24 57.70 37.20 96.54 117.34 531.75 510.19 483.50 179.23 176.82 264.62 244.25 223.79 273.31 ", " 551.88 377.00 191.27 457.96 94.25 111.80 98.09 455.81 278.93 362.35 134.53 652.92 466.59 561.43 205.83 ", " 269.11 91.35 257.26 802.24 718.88 712.53 589.42 480.42 504.12 239.15 ", " 372.31 420.49 64.41 98.05 278.93 324.04 658.98 306.54 245.60 561.06 111.33 466.59 519.70 205.83 762.13 ", " 512.66 552.89 42.05 372.31 157.18 191.27 420.49 95.52 133.24 64.41 98.05 415.28 455.81 278.93 324.04 396.09 129.03 163.60 28.16 37.20 301.38 96.54 603.83 643.29 458.33 510.19 192.94 483.50 179.23 334.43 375.20 203.30 244.25 81.22 223.79 273.31 " }; double[] firsts = new double[] { 36.87817782917155, 551.881327823292, 269.1059270993487, 372.3130403303113, 512.6646077115134 };

for (int f = 0; f < files.length; f++) { String file = files[f]; Program prog = new Program(file, false); prog.compareDistances(); CompressedArray ca = prog.getArray();

boolean t1 = ca.getLength() == sizes[f]; boolean t2 = ca.toString().equals(solns[f]); boolean t3 = ca.getElement(0) == firsts[f];

if (t1 && t2 && t3) { System.out.println("Test " + (f+1) + " Passed"); } else { System.out.println("Test " + (f+1) + " Failed"); } } }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!