2

I have txt file which looks like this and i want to transform it to object and then fill sqlite db with it. When I'm trying to parse data, strange error has place.

[Dokument]  
DataWyst=14.01.15
SymbolKontrahenta=KUMA
[ZawartoscDokumentu]
[Poz1]
Nazwa=MML
Symbol=5902317000160   
CenaNetto=169
SymbolDostawcy=    69    659
[Poz2]
Nazwa=ŚMT
Symbol=5902317100433
CenaNetto=1430
SymbolDostawcy=    54    568
[Poz3]
Nazwa=tka
Symbol=1204
CenaNetto=1008
SymbolDostawcy=     7     78


[Dokument]
DataWyst=14.01.15
SymbolKontrahenta=EA
[ZawartoscDokumentu]
[Poz1]
Nazwa=dag
Symbol=5900643030141
CenaNetto=358
SymbolDostawcy=
[Poz2]
Nazwa=DANS
Symbol=59026990
CenaNetto=234
SymbolDostawcy=     4     18

I'm trying to parse this text to objects in that way:

int i_liczba_dokumentow = liczba_dokumentow.size();
    Dokument[] l_dok = new Dokument[i_liczba_dokumentow];
    System.out.println(i_liczba_dokumentow);
    try {
        Scanner s = new Scanner(new File("D:\\Temp.txt"));
        for (int i = 0; i < l_dok.length; i++) {

            while (s.hasNextLine()) {

                if (s.next() == "[Dokument]") {
                    l_dok[i] = new Dokument();
                    String line = s.nextLine();
                    if (line == "DataWyst=") {
                        String[] si = line.split("\\=");
                        l_dok[i].DataWyst = si[1];

                    }
                    if (s.next() == "SymbolKontrahenta=") {
                        String line2 = s.nextLine();
                        String[] si = line2.split("\\=");
                        l_dok[i].SymbolKontrahenta = si[1];
                    }
                    while (s.hasNextLine()) {
                        if (s.next() == "[Poz") {
                            l_dok[i].Elementy.add(new Element());
                        }
                        if (s.next() == "Nazwa=") {
                            String line2 = s.nextLine();
                            String[] si = line2.split("\\=");
                            l_dok[i].Elementy.get(i).Nazwa = si[1];
                        }
                        if (s.next() == "Symbol=") {
                            String line2 = s.nextLine();
                            String[] si = line2.split("\\=");
                            l_dok[i].Elementy.get(i).Symbol = si[1];
                        }
                        if (s.next() == "CenaNetto=") {
                            String line2 = s.nextLine();
                            String[] si = line2.split("\\=");
                            l_dok[i].Elementy.get(i).CenaNetto = si[1];
                        }
                        if (s.next() == "SymbolDostawcy=") {
                            String line2 = s.nextLine();
                            String[] si = line2.split("\\=");
                            l_dok[i].Elementy.get(i).SymbolDostawcy = si[1];
                        }
                    }

                }

            }
            s.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (int i = 0; i < l_dok.length; i++) {

        System.out.println("Pozycja: " + l_dok[i].DataWyst + " "
                + l_dok[i].SymbolKontrahenta);
        System.out.println("Element: " + l_dok[i].Elementy.get(i).Nazwa + " "
                + l_dok[i].Elementy.get(i).Symbol+ " "+l_dok[i].Elementy.get(i).CenaNetto+ " "+ l_dok[i].Elementy.get(i).SymbolDostawcy );
    }

But the loop shows me error like:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at TextFormatter.main(TextFormatter.java:90)

line 90 is if (s.next() == "[Dokument]") {

Thank you for your clues, advices and mostly for your time. If you don't know how to solve this task please vote for my post.

user3310883
  • 95
  • 1
  • 2
  • 9
  • Are you sure line 90 is s.hasNextLine()? Why doesn't Scanner.hasNextLine() appear in your stacktrace? – FrobberOfBits Apr 11 '14 at 15:14
  • 1
    Use this class if you want to implement in java API http://stackoverflow.com/a/15638381/2182351 . else use http://ini4j.sourceforge.net/index.html for reading ini file. Dont reinvent the wheel – Mani Apr 11 '14 at 15:18
  • Debug your code. You will learn the mistakes. – Mani Apr 11 '14 at 15:20
  • You close the Scanner in the `for` loop in the first iteration and then try to call `s.hasNextLine` on the closed instance in the 2nd iteration. And in general it would be easier to test and debug it if you could refactor the sample it into smaller and easily testable units of code. – Norbert Radyk Apr 11 '14 at 15:22
  • @FrobberOfBits line 90 was edited. Take a look at it. – user3310883 Apr 11 '14 at 15:35
  • @Mani I'm trying to use ini4j and I've got a question like, how can I make unique object when i dont have unique id. For example, all [Dokument] block didn't have id. It can work only when there is unique block ? How can I solve it ? – user3310883 Apr 12 '14 at 21:30

0 Answers0