Reputation: 5286
I have a loop that reads the number of lines in a text file when the program starts, and then depending on the amount of lines, it stores that many objects into a new (Vehicle[]) array (Maximum 4).
public boolean addVehicle(Vehicle[] Honda) throws FileNotFoundException
{
Scanner reader = new Scanner(file);
String strLine = "";
if(canAddVehicle() == true)
{
for(int i = 0; i < vehicles.length;i++)
{
System.out.println("This file is: " + file);
int counter = 0;
if(vehicles[i] == null)
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(this.file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
//Declare objects inside the array.
Honda[counter] = new Vehicle();
Honda[counter].readRecord(reader);
vehicles[counter] = Honda[counter];
counter++;
}
strLine = "";
//Close the input stream and scanner
reader.close();
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
break;
}
}
return true;
}
The part I'm having trouble with is this line:
if(vehicles[i] == null)
After the program starts, users can choose to add new Vehicles to the array. If you go through the code line by line, you can see that it starts of at i = 0, and let's say when the program first ran it found 2 lines of values, so it stored 2 objects into the array.
Values 0 and 1 are taken. That means when the user goes to add a new Vehicle, it will skip over if(vehicles[i] == null)
, because spot [0] is not null, it contains the values from the beginning of the program.
It then leads to the break;
and kicks you out of the method without going back through the for loop to check if there are any other null values in the array.
What could I possibly do here?
Upvotes: 0
Views: 134
Reputation: 5286
Thanks for the answers everyone. I was programming for around 12 hours and my mind was just dead when I asked the question. Not sure what I was doing. I've completed my code with this:
public boolean addVehicle(Vehicle[] Honda) throws FileNotFoundException
{
boolean found = false;
int position = 0;
if(canAddVehicle() == true)
{
for(int i = 0; i < vehicles.length && !found; i++)
{
if(vehicles[i] == null)
{
position = i;
found = true;
}
}
Scanner reader = new Scanner(file);
while(reader.hasNext())
{
Honda[position] = new Vehicle();
Honda[position].readRecord(reader);
vehicles[position] = Honda[position];
position++;
}
reader.close();
return true;
}
return false;
}
Upvotes: 0
Reputation: 16209
Your code really makes little sense. From what I understand of your problem description the code below might or might not be what you want to do:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class VehicleList {
public class Vehicle {
private final String brand;
private final String make;
private final String year;
public Vehicle(String[] args) {
if (args.length < 3) {
throw new IllegalArgumentException("Too few args: " + args.length);
}
this.brand = args[0];
this.make = args[1];
this.year = args[2];
}
@Override
public String toString() {
return String.format("%s %s %s", year, brand, make);
}
}
public List<Vehicle> readVehicles(String fileName) throws IOException {
List<Vehicle> vehicles = new ArrayList<Vehicle>();
System.out.println(String.format("Reading vehicles from %s:", fileName));
readVehicles(vehicles, new Scanner(new File(fileName)), false);
System.out.println(String.format("Reading vehicles from user:"));
readVehicles(vehicles, new Scanner(System.in), true);
return vehicles;
}
private void readVehicles(List<Vehicle> vehicles, Scanner scanner, boolean skipLineCheck) {
int count = 0;
while (skipLineCheck || scanner.hasNextLine()) {
String[] tokens = scanner.nextLine().split("\\s+");
if (tokens.length < 3) {
break;
}
vehicles.add(new Vehicle(tokens));
count++;
}
scanner.close();
System.out.println(String.format("Read %s vehicles", count));
}
public static void main(String[] args) throws IOException {
VehicleList instance = new VehicleList();
List<Vehicle> vehicles = instance.readVehicles("vehicles.txt");
System.out.println("Read the following vehicles:");
System.out.println(Arrays.toString(vehicles.toArray()));
}
}
The boolean skipLineCheck is needed to stop the scanner from reading past the last line in the file and throwing a NoSuchElementException. For user input we don't want to do this check, because it forces the user to give an extra RETURN to end the input.
To run this you need to create a file called "vehicles.txt" in your working directory with for example the following contents:
Volvo Station 2008
Audi A4 2009
Honda Civic 2009
Toyota Prius 2008
A test run gives output like below:
Reading vehicles from vehicles.txt
Read 4 vehicles
Reading vehicles from user
Nissan Micra 2002
BMW cabriolet 1996
Read 2 vehicles
Read the following vehicles:
[2008 Volvo Station, 2009 Audi A4, 2009 Honda Civic, 2008 Toyota Prius, 2002 Nissan Micra, 1996 BMW cabriolet]
Upvotes: 1
Reputation: 2068
Based off of this statement: It then leads to the break; and kicks you out of the method without going back through the for loop to check if there are any other null values in the array.
it sounds like you want a continue where the break is.
break will cause the for loop to break, while continue will cause the code in the loop to be read (from top to bottom) with i incremented by 1 (in this case)
Upvotes: 0
Reputation: 11543
If you format your source it will be easier to see where your break is currently located. Then try to think about how you would manually step through your program. That usually helps me. The you can decide if you want to break always in your loop or only if you just loaded the new vehicles.
Peter Lawrey gave a good comment to use a debugger, after figuring what you ant your program to do, if it does not behave as you expected, using a debugger (very easy in most IDEs) you can step through your program to see each action it takes and check the values of your variables at each step.
Upvotes: 1
Reputation: 611
two things, a. switch break to continue, and place break in the right place that you want it.
b. you should close your file stream if your finish using it because when u open a fStream it "holds the file open" and un useable until you close the fStream
Upvotes: 1
Reputation: 4995
Why have you got the break there at all - it's just going to make it do exactly what you describe. Remove it and all will be well.
Upvotes: 0