读取文本文件并创建两种类型的对象,但第一个对象类型创建两次

时间:2017-12-18 14:30:45

标签: java arraylist

我有一个对象'l​​iman',liman在ArrayLists中包含船只和容器。我正在尝试从文本文件中填充ArrayLists,如下所示:

  

1574683 Abyssinia
  8546279 Royal_Oak
  2454438 Oceanway
  9146578 Atilgan
  3378841 Albatros
  6562152 Imbat

     

1000085321 N N
  1000255963 N N
  1000511926 N N
  1000853210 N N
  1001279815 N N
  1001791741 N N
  1002388988 N N
  ...

此文件有6艘船,他们的IMO及其名称。它有624个容器,它们的序列号和两个功能。这是我的方法:

public static Liman readK( Liman liman){
    Scanner inputFileReader = null;
    try {

        inputFileReader = new Scanner(new File("C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\liman.txt"));
        String line;
        String[] lineCutter;

        while (inputFileReader.hasNextLine()){
            line = inputFileReader.nextLine();

            if(line.length() > 7){
                lineCutter = line.split(" ");
                if(lineCutter[0].length()== 7){ //trying to see if the line contains a ship

                    int imo = Integer.valueOf(lineCutter[0]);
                    String isim = lineCutter[1];
                    Gemi yeniGemi = new Gemi(imo,isim);
                    liman.Gemiler.add(yeniGemi);
                } else if(lineCutter[0].length() == 10){//trying to see if the line contains a container

                    long ser= Long.valueOf(lineCutter[0]);
                    String Konteyner_Ozelligi= lineCutter[1];
                    String Icerik_Ozelligi= lineCutter[2];
                    Konteyner yeniKonteyner = new Konteyner(ser,Konteyner_Ozelligi,Icerik_Ozelligi);
                    liman.Lkonteynerler.add(yeniKonteyner);
                }
             }
         }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //System.out.println(liman.Lkonteynerler);
        inputFileReader.close();
        return liman;
    }
}

此代码创建容器并将其添加到容器的liman的ArrayList中。这对船只来说是一样的,但它会造船两次。所以liman的ArrayList for ship的大小是12.我做错了什么?

0 个答案:

没有答案