ファイルを読み取り、それを整数に変換するメソッドを取得する際に問題が発生しました。プログラムの簡単な説明は以上です。これは基本的に、ロット内の車両をテキスト ファイルに書き留めて追跡するカー ディーラーの在庫です。プログラムが起動したら、ファイルを読み取り、現在のすべての車を配列に入れて表示できるようにする必要があります。次に、プログラムの残りの部分は、車の削除やニュースの追加などの他のことを行います。私がいる部分は、プログラムが最初に起動したときにファイルを読み取る必要があることですが、動作させることができないようです。
テキスト ファイルは合計 6 行で構成されています。最初に 4 つの数字、次にそれぞれ 2 つの単語。メソッドで最初の 4 行を読み取り、それらを整数に変換して一時配列に格納する必要があります。その後、次の 2 行を読み取り、一時配列にも格納します。その後、これらの保存された値をすべて取得し、コンストラクターに送信します。コンストラクターは Arraylist に格納され、いつでも Arraylist にアクセスできます。出力では、これはすべてうまくいきます。しかし、これを防ぐための障壁があるにもかかわらず、メソッドをもう一度実行したいと考えています。
これがコードです。それはクラスであり、メインプログラムではありません。コード内でできる限りプログラムを説明しようとします。
public class Vehicle {
//All the different private variables for the constructors and methods
private int intholder[], year, type, kilometres, price, loop;
private String make, model, myline, holder[];
//The Arraylist that the different vehicle objects will be stored
ArrayList<Vehicle> allCars = new ArrayList<Vehicle>();
//The Default constructor
public Vehicle(){
make = "Vehicle Make";
model = "Vehicle Model";
type = 0;
year = 0;
kilometres = 0;
price = 0;
}
//The constructor that has information sent to it
public Vehicle(int _type, int _year, int _kilometres, int _price, String _make, String _model){
make = _make;
model = _model;
type = _type;
year = _year;
kilometres = _kilometres;
price = _price;
}
//Text file information
/*
* CAR TYPE CODE:
* 1 - Sedan
* 2 - Truck
* 3 - Crossover
* 4 - SUV
* 5 - Sports
*
* There is a total of 6 lines for each car and are as follows
* 1 - int Type integer
* 2 - int Year
* 3 - int Kilometres
* 4 - int Asking price
* 5 - String Make
* 6 - String Model
*/
//The method in question. It reads through the file, converts the integers and stores them,
//stores the strings, and sends all the information to the constructor
public void readCars()throws IOException{
BufferedReader readFile = new BufferedReader(new FileReader("C:/Users/David/Desktop/FinalProject/Carlot.txt"));
//Setting the length of the temporary arrays
holder = new String[2];
intholder = new int[4];
//The main loop in the method.
do{
//Read the first 4 lines of the file and convert them to integers.
//The try catch shouldn't have to be there because the first 4 lines
//of the file are all numbers, but I put it in there to see when it was messing up.
for(int i = 0; i < 4; i++){
myline = readFile.readLine();
try{
intholder[i] = Integer.parseInt(myline);
}
catch(NumberFormatException e){
System.out.println(e);
}
//Had this in here to see how many lines down the file it would go before messing up.
System.out.println(myline);
}
//Loop to store the Strings
for(int i = 0; i < 2; i++){
myline = readFile.readLine();
holder[i] = myline;
System.out.println(myline);
}
//Sends all the data to the constructor
Vehicle V = new Vehicle(intholder[0], intholder[1], intholder[2], intholder[3], holder[0], holder[1]);
//Several if statements to determine which subclass of vehicle it is.
if(intholder[0]==1){
Sedan S = new Sedan();
allCars.add(S);
}
else if(intholder[0]==2){
Truck T = new Truck();
allCars.add(T);
}
else if(intholder[0]==3){
Crossover C = new Crossover();
allCars.add(C);
}
else if(intholder[0]==4){
SUV U = new SUV();
allCars.add(U);
}
else if(intholder[0]==5){
Sports P = new Sports();
allCars.add(P);
}
//Only break the loop if the myline equals null
}while(myline != null);
//if the loop breaks, close the file
readFile.close();
}
これで、どこが間違っているのかがわかったと思います。do/while の最後に、「myline」が null かどうかをチェックします。最後にファイルを読み取ったときはまだ文字列だったので、ループが続きます。最後にループを通過すると、すべてがnullになるため、整数を変換しようとするとエラーが発生します。しかし、ループの最後で次の行に行かずにファイルを読み取る方法がわかりません。テキストファイルはこんな感じ。
1
2007
150250
5000
Toyota
Corolla
2
2005
240400
4500
Chevorlet
Silverado
ループの最後にそれを読み取らせることはできません。ループが再起動されたときに次の行に移動し、すべてが破棄されるためです。
どんな助けでも大歓迎です、ありがとう!