我有一个循环,它在程序启动时读取文本文件中的行数,然后根据行数,它将许多对象存储到一个新的(Vehicle[])数组(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;
}
我遇到麻烦的部分是这一行:
if(vehicles[i] == null)
程序启动后,用户可以选择向数组中添加新的车辆。如果你逐行浏览代码,你可以看到它从i = 0开始,假设当程序第一次运行时,它找到了2行值,所以它将2个对象存储到数组中。取值为0和1。这意味着当用户去添加一辆新车时,它将跳过if(vehicles[i] == null)
,因为spot[0]不为空,它包含程序开始时的值。
然后指向break;
并将您踢出方法,而无需返回for循环来检查数组中是否有任何其他空值。我在这里能做什么呢?
两件事,A.将break切换为continue,并将break放置在你想要的位置。
b。你应该关闭你的文件流,如果你结束使用它,因为当你打开一个fStream它
如果你格式化你的源代码,它将更容易看到你的break当前所在的位置。然后尝试考虑如何手动逐步完成程序。这通常对我有帮助。你可以决定是要在循环中总是中断,还是只在加载新车辆时才中断。
Peter Lawrey给出了一个很好的评论,使用调试器,在确定你想让你的程序做什么之后,如果它不像你预期的那样表现,使用调试器(在大多数ide中非常容易),你可以逐步通过你的程序来查看它所采取的每个动作,并检查每一步变量的值。
你的代码真的没什么意义。根据我对你的问题描述的理解,下面的代码可能是你想要做的,也可能不是:
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()));
}
}
需要布尔值skipLineCheck来阻止扫描器读取文件中的最后一行并抛出NoSuchElementException。对于用户输入,我们不想做这个检查,因为它强制用户给出一个额外的RETURN来结束输入。
要运行这个命令,你需要在你的工作目录中创建一个名为"vehicles.txt"的文件,例如包含以下内容:
Volvo Station 2008
Audi A4 2009
Honda Civic 2009
Toyota Prius 2008
测试运行的输出如下所示:
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]
你为什么要在这里设置断点呢?它只会使它完全按照你所描述的去做。去掉它,一切都会好起来的。
基于这个语句:然后导致break;并且在不返回for循环检查数组中是否有其他空值的情况下将您踢出方法。
听起来你想在break的地方继续。
break将导致for循环中断,而continue将导致读取循环中的代码(从上到下),I加1(在本例中)
谢谢大家的回答。我编写了大约12个小时的程序,当我问这个问题的时候,我的大脑已经麻木了。不知道我在做什么。我已经完成了我的代码:
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;
}