从文本文件中读取和提取不同的数据并创建不同的对象

时间:2017-12-15 18:58:37

标签: java

我有三个文本文件。第一个看起来像这样:

  

1574683 Abyssinia 105 60 25 21 11

第二个看起来像这样:

  

2454438 Oceanway 112 52 20 5 8

但第三部分的一小部分看起来像这样(其余部分就是这样,没有什么不同):

  

9146578 Atilgan 98 59 18 15 20

  1000085320 N N
  1000255961 N N
  1000511923 N N
  1000853206 N N
  1001279810 N N
  1001791735 N N
  ......

我正在尝试从这些文件创建对象并将它们存储在arraylist中,前两个文件是成功的。但最后一行有点不同,因为每一行都是一个对象,但第一行是另一个必须包含其余对象的对象,同时也有一个arraylist。
这是我的代码:

String[] name_and_path_of_files = {
    "C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\1574683.txt",
    "C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\2454438.txt",
    "C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\9146578.txt"
};
    Scanner inputFileReader = null;
    Liman liman = new Liman();
try {
    for (String fileInfo : name_and_path_of_files) {

        inputFileReader = new Scanner(new File(fileInfo));

        int line_index = 0;

        while (inputFileReader.hasNext()) {
                //String separated_numbers[] = inputFileReader.nextLine().split(" ");
                //inputFileReader.useDelimiter("\\\\d+");
                int IMO = inputFileReader.nextInt();
                String isim = inputFileReader.next();
                int Max_Konteyner_sayisi = inputFileReader.nextInt();
                int Buyuk_Konteyner_sayisi = inputFileReader.nextInt();
                int Sogutuculu_Konteyner_sayisi = inputFileReader.nextInt();
                int Toksik_Taşıma_siniri = inputFileReader.nextInt();
                int Patlayıcı_Taşıma_siniri = inputFileReader.nextInt();

                Gemi yeniGemi = new Gemi(IMO, isim, Max_Konteyner_sayisi, Buyuk_Konteyner_sayisi, Sogutuculu_Konteyner_sayisi,Toksik_Taşıma_siniri, Patlayıcı_Taşıma_siniri);
                liman.gemiEkle(yeniGemi); //gemiEkle adds ships into limans arraylist 'Gemiler'.
                System.out.println(liman.Gemiler);
                if(line_index > 0){
                int Seri_Numarasi = inputFileReader.nextInt();
                String Konteyner_Ozelligi = inputFileReader.next();
                String Icerik_Ozelligi = inputFileReader.next();
                Konteyner yeniKonteyner = new Konteyner(Seri_Numarasi,Konteyner_Ozelligi,Icerik_Ozelligi);
                yeniGemi.konteynerEkle(yeniKonteyner);
                System.out.println(yeniGemi.Konteynerler.toString());
                }
            line_index++;
        }    
    }
    //System.out.println(liman.Gemiler);
} catch (Exception e) {
    e.printStackTrace();
} finally {
        inputFileReader.close();
}

几乎所有东西都可以工作,但是第三个文件,它成功获得了第一行,但是当它到达其他对象时,它在这一行上给出了一个InputMismatchException:

  

int Max_Konteyner_sayisi = inputFileReader.nextInt();

这是输出:

[IMO: 1574683, İsim: Abyssinia, 105, 60, 25, 11]] [IMO: 1574683, İsim: Abyssinia, 105, 60, 25, 11], IMO: 2454438, İsim: Oceanway, 112, 52, 20, 8]] [IMO: 1574683, İsim: Abyssinia, 105, 60, 25, 11], IMO: 2454438, İsim: Oceanway, 112, 52, 20, 8], IMO: 9146578, İsim: Atilgan, 98, 59, 18, 20]]

1 个答案:

答案 0 :(得分:1)

你只需要使用hasNextInt():到目前为止你可以从你的设计中做出来,你需要进行以下更改:

    String[] name_and_path_of_files = {
        "C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\1574683.txt",
        "C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\2454438.txt",
        "C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\9146578.txt"
    };
        Scanner inputFileReader = null;
        Liman liman = new Liman();
    try {
        for (String fileInfo : name_and_path_of_files) {

            inputFileReader = new Scanner(new File(fileInfo));

            int line_index = 0;
            Gemi yeniGemi = null; //---declare your arraylist-containing object here.
            while (inputFileReader.hasNext()) {
                    //String separated_numbers[] = inputFileReader.nextLine().split(" ");
                    //inputFileReader.useDelimiter("\\\\d+");
                    int IMO = inputFileReader.nextInt();
                    String isim = inputFileReader.next();
                    if(inputFileReader.hasNextInt()){
                        int Max_Konteyner_sayisi = inputFileReader.nextInt();
                        int Buyuk_Konteyner_sayisi = inputFileReader.nextInt();
                        int Sogutuculu_Konteyner_sayisi = inputFileReader.nextInt();
                        int Toksik_Taşıma_siniri = inputFileReader.nextInt();
                        int Patlayıcı_Taşıma_siniri = inputFileReader.nextInt();
                        yeniGemi = new Gemi(IMO, isim, Max_Konteyner_sayisi, Buyuk_Konteyner_sayisi,
                                Sogutuculu_Konteyner_sayisi,Toksik_Taşıma_siniri, Patlayıcı_Taşıma_siniri)
                        liman.gemiEkle(yeniGemi); //gemiEkle adds ships into limans arraylist 'Gemiler'.
                        System.out.println(liman.Gemiler);
                    }
                    else{
                        //int Seri_Numarasi = inputFileReader.nextInt();//---why repeat when you have retrived this line's leading values?
                        //String Konteyner_Ozelligi = inputFileReader.next();
                        String Icerik_Ozelligi = inputFileReader.next();
                        Konteyner yeniKonteyner = new Konteyner(IMO,isim,Icerik_Ozelligi);
                        yeniGemi.konteynerEkle(yeniKonteyner);
                        System.out.println(yeniGemi.Konteynerler.toString());
                        }
            }    
        }
        //System.out.println(liman.Gemiler);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
            inputFileReader.close();
    }