At the moment in my programming course we are learning about I/O streams. Unfortunately my teacher has decided not to come to the past two labs, and has canceled the last lecture, leaving my class and me on our own for learning the basics of I/O streams for our latest assignment.
For our latest assignment we are to read information from a text file "ListOfAirlines.txt" and store them inside of three different classes (Location, Designator, and Airline). The Airline class is to have objects of both the Location and Designator classes.
The problem is that my program does'nt seem to want to read the input from the file. Here is my code for the program so far. It's not done, but I tried testing out what I have so far just to see if it works. Every time I run it, the catch block is the only part that runs despite me typing in the exact file name. I'm also getting a runtime error saying that there is an issue on line 39 (where the for loop starts). I really have no idea what I'm doing and I've mostly just been trying to follow examples in my book for this particular program.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class AirlineOutput {
public static void storeInfo() {
AirlineInput object = new AirlineInput();
String file = AirlineInput.enterFileName();
Scanner inputStream = null;
try {
inputStream = new Scanner(new File(file));
} catch (FileNotFoundException z) {
System.out.println("Error opening the file "+file);
}
/*
* The following creates an array for every object in the program.
* It cycles through the input stream assigning each line to a String variable.
* The String variables are then passed through to their respective object classes.
* The Location and Designator objects are passed to the airline object as the
* airline object is required to contain it's own Location and Designator objects.
*/
for (int i = 0; inputStream.hasNextLine(); i++) {
Airline[] airline = new Airline[i];
airline[i]=new Airline();
Designator[] designator = new Designator[i];
designator[i]=new Designator();
Location[] location = new Location[i];
location[i]=new Location();
String stringRead = inputStream.nextLine();
Scanner parse = new Scanner(stringRead);
String airlineName = parse.next();
String designators = parse.next();
String frequentFlyerProgram = parse.next();
String city = parse.next();
String state = parse.next();
String country = parse.next();
String website = parse.next();
location[i].setCity(city);
location[i].setCountry(country);
location[i].setState(state);
airline[i].setLocation(location[i]);
designator[i].setDesignators(designators);
airline[i].setDesignators(designator[i]);
airline[i].setName(airlineName);
airline[i].setWebsite(website);
airline[i].setFrequentFlyer(frequentFlyerProgram);}
inputStream.close();
}
}
}
the .enterFileName() is under the the input class that we are required to have. It's just to ask the user for the filename which is then stored in a string.
import java.util.Scanner;
public class AirlineInput {
static String file;
public AirlineInput() {
}
public static String enterFileName() {
System.out.println("Enter the file name: ");
Scanner keyboard=new Scanner(System.in);
file=keyboard.next();
return file;
}
}
Java often gives a preety good feedback, which is not always easy to notice at first glance when you learn the language :)
Quoting your question:
Every time I run it, the catch block is the only part that runs
Which gives feedback that exception of class FileNotFoundException is being caught - error occured and try-catch block prevented application from crashing.
Extract from the documentation says that this exception will be thrown:
So in this case one of above during code execution occurs for 100%.
After exception was caught the program proceeds and line with code:
for(int i=0;inputStream.hasNextLine();i++)
makes the program crash because variable inputStream has no value assigned, and calling methods on unassigned variables probably provides you with another exception.
I hope my answer helps and bon voyage on java path :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With