Running into a problem with my program.
Hello, im having trouble with getting this program I am writing for one of my courses to run. The program has a 4 different classes. I am having trouble getting the program to read input from a file, having it take those items and creating vehicle objects, and then putting those vehicle objects into an array. The code I am providing below is only part of the program but should be enough to help me with the problem im having so far.
the file it is reading from(fleetCars.txt) contains this information. There is a tab between each item for each car.
Hyundai Sonata 2010 ABC236347NM2N2NW2 18455.34 8765 7567
Chevy Blazer 1998 1234H32343LMN3423 29556.65 38559 38559
Code :
public class Fleet
{
Vehicle[] fleetVehicles = new Vehicle[4];
public Fleet()
{
super();
}
public Fleet(String fileName) throws IOException
{
Scanner inputFile = new Scanner(fileName);
for (int i = 0; i < fleetVehicles.length; i++)
{
Vehicle vehobj = new Vehicle();
vehobj.readRecord(inputFile);
fleetVehicles[i] = vehobj;
}
}
public String toString()
{
Vehicle obj = new Vehicle();
for (int i = 0; i < fleetVehicles.length; i++)
{
fleetVehicles[i] = obj;
return obj.toString();
}
return obj.toString();
}
Code :
public class Vehicle
{
private String vin;
private String make;
private String model;
private int year;
private double value;
private int milesDriven;
private int lastOilChange;
public Vehicle()
{
super();
}
public Vehicle(String vin, String make, String model, int year,
double value, int milesDriven, int lastOilChange)
{
this(vin, make, model, year, value);
this.milesDriven = milesDriven;
this.lastOilChange = lastOilChange;
}
public String toString()
{
return "Make:\t\t" + this.make + "\nModel:\t\t" + this.model
+ "\nYear:\t\t" + this.year + "\nValue:\t\t" + this.value
+ "\nVIN:\t\t" + this.getVIN() + "\nMiles Driven: "
+ this.milesDriven + "\nNext oil change at "
+ this.getMilesToNextOilChange() + " miles";
}
public void readRecord(Scanner inputFile) throws IOException
{
make = inputFile.next();
model = inputFile.next();
year = inputFile.nextInt();
vin = inputFile.next();
value = inputFile.nextInt();
milesDriven = inputFile.nextInt();
lastOilChange = inputFile.nextInt();
}
The last bit of code is the part that runs the whole program. This part asks the user for the name of the file. It then is supposed to open the file and use it to build the fleet object.
Code :
public class FleetInterfaceApp
{
private Fleet fleet;
private Menu menu;
private void createMenu()
{
String[] choices =
{ "Add a vehicle", "Delete a vehicle", "Update miles driven",
"Record oil change", "Print listing of all fleet vehicles",
"Print listing of all vehicles to be replaced",
"Print listing of all vehicles that need and oil change",
"Quit" };
menu = new Menu(choices);
}
public void run() throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out
.println("What is the name of the file containing the fleet vehicle information?");
String fileName = keyboard.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
Fleet fleet = new Fleet(fileName);
int choice;
this.createMenu();
choice = this.menu.getChoice();
System.out.println("You entered choice " + choice);
if (choice == 1)
choice1();
if (choice == 2)
choice2();
if (choice == 3)
choice3();
if (choice == 4)
choice4();
if (choice == 5)
choice5();
if (choice == 6)
choice6();
if (choice == 7)
choice7();
}
This is the error I get when I run fleetInterFaceApp
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Vehicle.readRecord(Vehicle.java:285)
at Fleet.<init>(Fleet.java:51)
at FleetInterfaceApp.run(FleetInterfaceApp.java:47)
at FleetInterfaceApp.main(FleetInterfaceApp.java:166)
Thanks in advance for any help!
Re: Running into a problem with my program.
Code practices aside, I'd be willing to bet that the reason you're getting that error is because of the blank line in-between your data in the file.
Remove all empty lines from the file and then return detailing what other errors you get :).
Re: Running into a problem with my program.
Thats not how it is in the file. I just realized i hit paste twice sorry. What else could it be?
Re: Running into a problem with my program.
In that case, you've only got 2 lines of data, but attempting to retrieve four lines worth of data.
Code java:
//fleetVehicles.length is 4!
for (int i = 0; i < fleetVehicles.length; i++)
{
//readFile
}
You should really be incorporating the hasNext() methods of the Scanner API, so it checks whether there are more tokens to process before trying to retrieve them.
Have a look:http://docs.oracle.com/javase/1.5.0/...l/Scanner.html
Re: Running into a problem with my program.
It still does it even if I change the array to 2.
Should I be using the has next inside the Fleet object constructor or where at?
I tried this but it still didnt work.
Code :
public Fleet(String fileName) throws IOException
{
Scanner inputFile = new Scanner(fileName);
int counter = 0;
while (inputFile.hasNext())
{
Vehicle vehobj = new Vehicle();
vehobj.readRecord(inputFile);
fleetVehicles[counter] = vehobj;
counter++;
}
Re: Running into a problem with my program.
The 5th input you read in, is as follows, 18455.34
The line of code used to load that is,
value = inputFile.nextInt();
Do you see the potential problem?
Re: Running into a problem with my program.
Thanks Newbie and Freaky Chris. I did not realize that I had put nextInt and not nextDouble. I fixed that and the program is still giving me this error.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Vehicle.readRecord(Vehicle.java:296)
at Fleet.<init>(Fleet.java:50)
at FleetInterfaceApp.run(FleetInterfaceApp.java:47)
at FleetInterfaceApp.main(FleetInterfaceApp.java:166)
Any other suggestions?
Re: Running into a problem with my program.
How far exactly does it get, does it read one line then crash on the second?
Which is the exact line of code it crashes on, there are multiple .next() calls in that method and I don't know which it is.
Chris
Re: Running into a problem with my program.
Well now you see, it still tries to access data which isn't there.
Using while (inputFile.hasNext()), when it reaches the very last token, it will still call your readRecord() method, where it will invoke .next() 7 times.
Re: Running into a problem with my program.
I don't believe that it even starts to read them. I tried using the debugger and it ends up saying source not found with a button that says edit source look up path...
Im really stumped on this... any other ideas?