2017-04-20 19 views
-2

添加列表我有这个文件:如何在列表

MERCEDES 320000 3.5 37000 ABS;GPS;RADIO 

我想制造从中对象。 我做的:

private List<Car> cars = new ArrayList<>(); 
public listOfCars(String fileName){ 
    try{ 
     FileReader fr = new FileReader(fileName); 
     Scanner sc = new Scanner(fr); 

     String[] verse = null; 

     int count=0; 
     while(sc.hasNextLine()){ 
      verse = sc.nextLine().split(" "); 
      String[] components = verse[4].split(";"); 
      List<String> eq = new ArrayList<>(); 
      eq.addAll(Arrays.asList(components)); 
      cars.add(count, new Car(verse[0], Double.parseDouble(verse[1]), Double.parseDouble(verse[2]), Double.parseDouble(verse[3]), eq)); 
      count++; 
     } 

     sc.close(); 
    }catch(FileNotFoundException e){ 
     e.printStackTrace(); 
    } 
} 

当我做的toString我可以看到:

listofCars{cars=[Car{m='MERCEDES', c=320000.0, p=3.5, pp=37000.0}]} 

但它应该是:

listofCars{cars=[Car{m='MERCEDES', c=320000.0, p=3.5, pp=37000.0, [ABS, GPS, RADIO}]} 

我怎么能这样做?我在哪里犯错?

+2

显示类'car' – Jens

+2

很可能是因为您在其中没有显示'Car.toString()'的错误。我会逐个调试器中的代码来确认这一点。 –

回答

0
class ABS {public ABS() {};}; 
class GPS {public GPS() {};}; 
class RADIO {public RADIO() {};}; 

class Car { 
    public String m; //Brand/Marke 
    public double c; //Kilometres/Kilometer 
    public double p; //Something 
    public double pp; //Something 
    public List<Object> features; //Extras 
    public Car(String m,double c,double p,double pp,List<Object> features) { 
     this.m=m; 
     this.c=c; 
     this.p=p; 
     this.pp=pp; 
     this.features=features; 
    } 
    public String toString() { 
     String s="Car{m="+m+",c="+Double.toString(c)+",p="+Double.toString(p)+",pp="+Double.toString(pp)+",features=["; 
     for (int i=0; i < features.length; i++) { 
      Object o=features.get(i); 
      if (Object.getClass()==ABS.class) { 
       s+="ABS"; 
      } 
      if (Object.getClass()==GPS.class) { 
       s+="GPS"; 
      } 
      if (Object.getClass()==RADIO.class) { 
       s+="RADIO"; 
      } 
      if (i!=features.length-1) { 
       s+=","; 
      } 
     } 
     return s+"]}" 
    } 
} 
public class App { 
private List<Car> cars = new ArrayList<>(); 
public listOfCars(String fileName){ 
try{ 
    FileReader fr = new FileReader(fileName); 
    Scanner sc = new Scanner(fr); 

    String[] verse = null; 

    int count=0; 
    while(sc.hasNextLine()){ 
     verse = sc.nextLine().split(" "); 
     String[] components = verse[4].split(";"); 
     List<Object> eq = new ArrayList<>(); 
     for (String s:components) { 
      if (s.equals("ABS") { 
       eq.add((Object)new ABS()); 
      } 
      if (s.equals("RADIO") { 
       eq.add((Object)new RADIO()); 
      } 
      if (s.equals("GPS") { 
       eq.add((Object)new GPS()); 
      } 
     } 
     cars.add(count, new Car(verse[0], Double.parseDouble(verse[1]), Double.parseDouble(verse[2]), Double.parseDouble(verse[3]), eq)); 
     count++; 
    } 

    sc.close(); 
}catch(FileNotFoundException e){ 
    e.printStackTrace(); 
} 
} 
public static void main(String[] args) throws IOException { 
      listOfCars("list.txt"); 
     } 

    } 

使用字符串:

class Car { 
    public String m; //Brand/Marke 
    public double c; //Kilometres/Kilometer 
    public double p; //Something 
    public double pp; //Something 
    public List<String> features; //Extras 
    public Car(String m,double c,double p,double pp,List<String> features) { 
     this.m=m; 
     this.c=c; 
     this.p=p; 
     this.pp=pp; 
     this.features=features; 
    } 
    public String toString() { 
     String s="Car{m="+m+",c="+Double.toString(c)+",p="+Double.toString(p)+",pp="+Double.toString(pp)+",features=["; 
     for (int i=0; i < features.length; i++) { 
      s+=features.get(i); 
      if (i!=features.length-1) {s+=","; 
      } 
     } 
     return s+"]}" 
    } 
} 
public class App { 
private List<Car> cars = new ArrayList<>(); 
public listOfCars(String fileName){ 
try{ 
    FileReader fr = new FileReader(fileName); 
    Scanner sc = new Scanner(fr); 

    String[] verse = null; 

    int count=0; 
    while(sc.hasNextLine()){ 
     verse = sc.nextLine().split(" "); 
     String[] components = verse[4].split(";"); 
     List<Object> eq = new ArrayList<>(); 
     for (String s:components) { 
      eq.add(s); 
     } 
     cars.add(count, new Car(verse[0], Double.parseDouble(verse[1]), Double.parseDouble(verse[2]), Double.parseDouble(verse[3]), eq)); 
     count++; 
    } 

    sc.close(); 
}catch(FileNotFoundException e){ 
    e.printStackTrace(); 
} 
} 
public static void main(String[] args) throws IOException { 
     listOfCars("list.txt"); 
    } 

} 
+0

这是如何回答这个问题的?这里增加了很多行。 – AxelH

+0

@AxelH:他没有提供他的汽车课。 – user7185318

+0

那你给他提供了吗?除非你认识他或者行使,否则我不知道你怎么能确定这是他需要的。 – AxelH