Reputation: 9
When I run program it never stops, and don't know how to make (for each line) different v number (gives me syntax error) thanks for helping. file format each line has string int string string
public static void main(String[] args) throws FileNotFoundException, IOException {
String filelocation = "location";
filelocation = JOptionPane.showInputDialog("Enter file address");
BufferedReader inFile = new BufferedReader(new FileReader(filelocation)); //using BufferedReader to get readLine function
ArrayList<Vehicle> VehicleList = new ArrayList<Vehicle>();
int counter = 1;
String dataRow = null;
dataRow= inFile.readLine();
while ( dataRow != null){
try{
String[] temp = dataRow.trim().split("\\s+");
Vehicle v(counter) = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);
VehicleList.add(v1);
dataRow= inFile.readLine();
}
catch(Exception e){}
}
inFile.close();
System.out.println(VehicleList);
System.out.println(v1);
}
Upvotes: 0
Views: 359
Reputation: 1989
The most likely cause to the loop not terminating is an exception within the read loop (you consume the exception and try to continue). Based on your example, I think that you have a mismatch on input from split versus the Vehicle constructor. Also as you found out, you cannot have dynamic variable names such that Vehicle v(counter) = new Vehicle(... gives a syntax exception.
See if this version helps show you where the problem is.
public static void main(String[] args) throws FileNotFoundException, IOException {
String filelocation = "location";
filelocation = JOptionPane.showInputDialog("Enter file address");
BufferedReader inFile = new BufferedReader(new FileReader(filelocation)); //using BufferedReader to get readLine function
ArrayList<Vehicle> VehicleList = new ArrayList<Vehicle>();
int counter = 0;
String dataRow = null;
dataRow= inFile.readLine();
try{
while ( dataRow != null){
String[] temp = dataRow.trim().split("\\s+");
if (temp.length != 5)
throw new Exception("split returned "+temp.length);
Vehicle v = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);
VehicleList.add(v);
System.out.println(v);
dataRow= inFile.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
finally {
inFile.close();
}
System.out.println(VehicleList);
}
Upvotes: 0
Reputation: 1877
As has been been said, you are most likely getting an exception within your while loop.
But the main problem with that is dataRow = inFile.readLine()
being in the while loop.
while ( dataRow != null){
try{
String[] temp = dataRow.trim().split("\\s+");
Vehicle v(counter) = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);
VehicleList.add(v1);
dataRow= inFile.readLine(); // <---shouldn't be in while loop
}
catch(Exception e){}
}
inFile.close();
System.out.println(VehicleList);
System.out.println(v1);
}
So what happens is, an exception is thrown, and then jumps to the catch block without ever changing the value of dataRow
!
So everytime the while loop is executed, it is actually comparing dataRow
to the first value you set it as.
You can get around this by changing the code as in the example below:
while((dataRow = inFile.readLine()) != null){
try {
//some stuff
}
catch(Exception e){
//some other stuff
}
}
Make sure to remove the line you have before the while loop that is dataRow = inFile.readLine()
So with this, it will behave more or less how you wanted it to. If you wanted to ignore the thrown exceptions, this will continue to process the file until the end. The line where the exception was thrown will be skipped.
Upvotes: 1
Reputation: 12196
You can't assign Dynamic variable names
. Java is a strongly type language.
What you can do, is save it on a list, and use the index for getting and setting it.
Your code should be as follow:
String[] temp = dataRow.trim().split("\\s+");
Vehicle v = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);
VehicleList.add(v);
System.out.println(v);
dataRow= inFile.readLine();
For getting use
VehicleList.get(index)
For setting use
VehicleList.set(index, VEHICLE)
Upvotes: 0