Elif Şahin
Elif Şahin

Reputation: 33

Reading and extracting different data from a text file and creating different objects

I have three text files. The first one looks like this:

1574683 Abyssinia 105 60 25 21 11

The second one looks like this:

2454438 Oceanway 112 52 20 5 8

But a small part of the third one looks like this(the rest of it goes just like that, nothing different):

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
...

I'm trying to create objects from these files and store them in an arraylist, the first two files are successful. But the last one is a bit different because each line is an object but the first line is another object that has to contain rest of the objects, with an arraylist as well.
Here's my code:

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();
}

Pretty much everything works but the third file, it gets the first line successfully but when it gets to the other objects it gives an InputMismatchException on this line:

int Max_Konteyner_sayisi = inputFileReader.nextInt();

Here's the output:

[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]]

Upvotes: 1

Views: 49

Answers (1)

dev8080
dev8080

Reputation: 4020

You need only use hasNextInt(): from what i could make out of your design so far, you need the following changes:

    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();
    }

Upvotes: 1

Related Questions