私はJavaを使い慣れていないため、テキストファイルから行ごとに車両を読み込み、車、トラック、自転車、アメリカ車、外国車)は、これらの車の後ろの線を通り、車を物に変換するコンストラクトに渡して、それらのオブジェクトを配列に入れることができます。車両は、お互いからテキストファイルの特定のレイアウトで拡張されます。私は、これらの行をコンストラクタに渡してコンストラクタからオブジェクトを返す方法を考え出すのに問題があります。次に、テキストファイルのレイアウトの例を示します。テキストファイルから行単位で車両を読み込み、Javaのコンストラクタにそれらの車両を渡す
vehicle
owner's name (string)
address (string)
phone (string)
email (string)
car - extends of vehicle
owner's name (string)
address (string)
phone (string)
email (string)
true or false for convertible (boolean)
color (string)
american car - extends of car
owner's name (string)
address (string)
phone (string)
email (string)
true or false for convertible (boolean)
color (string)
true or false for made in Detroit (boolean)
true or false for union shop (boolean)
foreign car - extends of car
owner's name (string)
address (string)
phone (string)
email (string)
true or false for convertible (boolean)
color (string)
country of manufacturer (string)
import duty (float)
bicycle - extends of vehicle
owner's name (string)
address (string)
phone (string)
email (string)
number of speeds (int)
truck - extends of vehicle
owner's name (string)
address (string)
phone (string)
email (string)
number of tons (float)
cost of truck (float)
date purchased (format below in example)
また、ここで私が使用していたサンプルデータさ:
foreign car
aMarioy
Mario's house
(777) 777-7777
[email protected]
false
black
Italy
4415.91
truck
aDougy
Doug's house
(123) 456-7890
[email protected]
30
61234.56
8/10/2003
vehicle
aRobby
Rob's house
(987) 654-3210
[email protected]
bicycle
bTommy
Tom's house
(246) 810-1214
[email protected]
7
truck
bGeorge
George's house
(666) 666-6666
[email protected]
25
51234.56
12/4/2004
vehicle
bTim
Tim's house
(111) 111-1111
[email protected]
bicycle
bJim
Jim's house
(555) 555-5555
[email protected]
5
american car
bJohn
John's house
(888) 888-8888
[email protected]
true
green
false
true
car
cKen
Ken's house
(999) 999-9999
[email protected]
false
orange
foreign car
cMario
Mario's house
(777) 777-7777
[email protected]
false
black
Italy
4415.91
truck
zDoug
Doug's house
(123) 456-7890
[email protected]
30
61234.56
8/10/2003
vehicle
eRob
Rob's house
(987) 654-3210
[email protected]
bicycle
fTom
Tom's house
(246) 810-1214
[email protected]
7
american car
gSam
Sam's house
(333) 333-3333
[email protected]
false
blue
true
false
をここでは私のコードです...
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
public class test4{
public static void main(String[] args)throws FileNotFoundException {
try{
Scanner file = new Scanner(new File(args[0]));
//ArrayList<Vehicle> vehicleList = new ArrayList<>();
String line = new String();
String newLine = new String();
ArrayList<String> s1List = new ArrayList<>();
ArrayList<String> s2List = new ArrayList<>();
ArrayList<String> s3List = new ArrayList<>();
ArrayList<String> s4List = new ArrayList<>();
ArrayList<String> s5List = new ArrayList<>();
ArrayList<String> s6List = new ArrayList<>();
while(file.hasNextLine()){
line = file.nextLine();
if(line.equals("vehicle")){
while(file.hasNextLine()){
newLine = file.nextLine();
if(!(newLine.equals(""))){
s1List.add(newLine);
}
if(newLine.equals("")){
break;
}
}
}
if(line.equals("truck")){
while(file.hasNextLine()){
newLine = file.nextLine();
s2List.add(newLine);
if(newLine.equals("")){
break;
}
}
}
if(line.equals("bicycle")){
while(file.hasNextLine()){
newLine = file.nextLine();
s3List.add(newLine);
if(newLine.equals("")){
break;
}
}
}
if(line.equals("car")){
while(file.hasNextLine()){
newLine = file.nextLine();
s4List.add(newLine);
if(newLine.equals("")){
break;
}
}
}
if(line.equals("american car")){
while(file.hasNextLine()){
newLine = file.nextLine();
s5List.add(newLine);
if(newLine.equals("")){
break;
}
}
}
if(line.equals("foreign car")){
while(true){
newLine = file.nextLine();
s6List.add(newLine);
if(newLine.equals("")){
break;
}
}
}
}
/*
String[] s1 = s1List.toArray(new String[0]);
String[] s2 = s2List.toArray(new String[0]);
String[] s3 = s3List.toArray(new String[0]);
String[] s4 = s4List.toArray(new String[0]);
String[] s5 = s5List.toArray(new String[0]);
String[] s6 = s6List.toArray(new String[0]);
*/
/*
System.out.println(s1.length);
for(String a: s1){
System.out.println(a);
}
*/
/*
for(String a: s1List){
System.out.println(a);
}
for(String a: s2List){
System.out.println(a);
}
for(String a: s3List){
System.out.println(a);
}
for(String a: s4List){
System.out.println(a);
}
for(String a: s5List){
System.out.println(a);
}
for(String a: s6List){
System.out.println(a);
}
*/
}catch(FileNotFoundException ex){
System.out.println("File not found.");
}
}
public boolean equals(Object o){
if(this == o){
return true;
}
if(o == null){
return false;
}
return false;
}
}
class Vehicle{
private String owner = new String();
private String address = new String();
private String phone = new String();
private String email = new String();
public Vehicle(String[] strArr){
owner = strArr[0];
address = strArr[1];
phone = strArr[2];
email = strArr[3];
}
}
私は現在、テキストをスキャンするプログラムを持っています車両のためのファイルを作成し、空の行に停止するarraylistsに順次線を格納します。私は車両が常に特定のレイアウトであるが、異なる順序になるので、arraylistからコンストラクタに特定のインデックスを渡す方法はわかりません。私はarraylistのインデックスを数えて、コンストラクタに渡すことができるものを見つけることができます。
ありがとうございました。
こんにちは。私はあなたのif(String.IsNullOrEmpty(line))ステートメントのロジックを理解していません。その声明は、スイッチを内部で実行していない時間のほとんどを間違って返すのではないでしょうか? –
if文は、テキストファイルに空行が見つかるたびにtrueと評価されます。これは、新しいビークルが定義されている場合も同様です。しかし、その時点まで、その空の線の前のすべてが単一のビークルのプロパティを構成します。だから、私がやっているのは、リスト内のすべてのプロパティを集めることです。次の行が空のときは、次の車両のプロパティを収集できるように、新しい車両オブジェクトを作成してプロパティリストを空にする –
こんにちは。車のプロパティが終了した時を知るために空きスペースをチェックしているので、空きスペースとファイルの終わりをどうやって確認できますか?なぜなら、テキストファイルが空のスペースで終わらないと、最後のビークルをコンストラクタに渡さないからです。 –