【发布时间】:2017-02-25 05:37:54
【问题描述】:
我是 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
gmario@mario.com
false
black
Italy
4415.91
truck
aDougy
Doug's house
(123) 456-7890
hdoug@doug.com
30
61234.56
8/10/2003
vehicle
aRobby
Rob's house
(987) 654-3210
irob@rob.com
bicycle
bTommy
Tom's house
(246) 810-1214
jtom@tom.com
7
truck
bGeorge
George's house
(666) 666-6666
kgeorge@george.com
25
51234.56
12/4/2004
vehicle
bTim
Tim's house
(111) 111-1111
tim@tim.com
bicycle
bJim
Jim's house
(555) 555-5555
Ajim@jim.com
5
american car
bJohn
John's house
(888) 888-8888
Bjohn@john.com
true
green
false
true
car
cKen
Ken's house
(999) 999-9999
Cken@ken.com
false
orange
foreign car
cMario
Mario's house
(777) 777-7777
Dmario@mario.com
false
black
Italy
4415.91
truck
zDoug
Doug's house
(123) 456-7890
Edoug@doug.com
30
61234.56
8/10/2003
vehicle
eRob
Rob's house
(987) 654-3210
Frob@rob.com
bicycle
fTom
Tom's house
(246) 810-1214
Gtom@tom.com
7
american car
gSam
Sam's house
(333) 333-3333
Hsam@sam.com
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];
}
}
我目前让程序扫描文本文件中的车辆,然后将顺序行存储到一个在空行处停止的数组列表中。我不确定如何将特定索引从数组列表传递给构造函数,因为我在想,因为车辆总是处于特定布局但顺序不同。我可以计算数组列表中的索引,以确定我可以传递给构造函数的内容。
感谢您的帮助。
【问题讨论】:
标签: java arrays string object constructor