简体   繁体   中英

Reading from a file and splitting the line

Here the instructions for the last part of my Bookstore exercize:

Through the read() method of Bookstore class it is possible to read the information about publishers and books and store it in the respective collections. This method receives as argument the file path. The file is a text organized in lines with fields, separated from each other by ';'. Every line contains information about either a publisher or a book, if the first field contains letter "P" or "B" respectively. Publisher lines contain, after the first one, the fields name, average delivery and email. Book lines contain, after the first one, the fields title, author, year, price, publisher name, and the quantity. The method must ignore lines which are not conform to this described pattern. <

And here the code I created until now:

public void read(String dir) throws IOException, FileNotFoundException {
    String s;
    String name="", del="", email="";
    int delivery=0;
    String author="", title="", publisher="", yr="", qnt="", prc="";
    int year=0, quantity=0;
    double price=0.0;
    int ch = 0;
    BufferedReader br = new BufferedReader(new FileReader(dir));

    while((s=br.readLine())!=null){

        // Here I know I should put more conditions but I can't define in my mind "field" in a right way.

        if((s.charAt(0)!='P' && s.charAt(1)!=';') || (s.charAt(0)!='B' && s.charAt(1)!=';'))
            System.out.println("Line not conform");

        else if((s.charAt(0)=='P') && (s.charAt(1)==';')){
            for(int i=2; s.charAt(i)!=';'; i++){
                name += s.charAt(i);
                ch = i+2;
            }
            for(int i=ch; s.charAt(i)!=';'; i++){
                del += s.charAt(i);
                delivery = Integer.parseInt(del);
                ch = i+2;
            }
            for(int i=ch; i<s.length(); i++){
                email += s.charAt(i);
            }
            publishers.add(new Publisher(name, delivery, email));
            name=""; del=""; email="";
            ch=0; delivery=0;

        } else if((s.charAt(0)=='B') && (s.charAt(1)==';')){
            for(int i=2; s.charAt(i)!=';'; i++){
                title += s.charAt(i);
                ch = i+2;
            }
            for(int i=ch; s.charAt(i)!=';'; i++){
                author += s.charAt(i);
                ch = i+2;
            }
            for(int i=ch; s.charAt(i)!=';'; i++){
                yr += s.charAt(i);
                year = Integer.parseInt(yr);
                ch = i+2;
            }
            for(int i=ch; s.charAt(i)!=';'; i++){
                prc += s.charAt(i);
                price = Double.parseDouble(prc);
                ch = i+2;
            }
            for(int i=ch; s.charAt(i)!=';'; i++){
                publisher += s.charAt(i);
                ch = i+2;
            }
            for(int i=ch; i<s.length(); i++){
                qnt += s.charAt(i);
                quantity = Integer.parseInt(qnt);
            }

            Book book=null;
            for(int i=0; i<publishers.size(); i++){
                if(publisher.equals(publishers.get(i).getName())){
                    book = new Book(title, author, year, price, publisher);
                    books.add(book);
                }
            }
            libro.setQuantity(quantity);
            author=""; title=""; publisher=""; prc=""; yr=""; qnt="";
            ch=0; year=0; price=0; quantity=0;
        }
    }
}

As far as I am now, I can correctly store publishers and books, but then I fail the last test because I don't know how to express the concept of field with the right code. Can anyone help me, please?

Moreover, I am sure there is a simpler way to write what I wrote, but this seemed to me the best solution at the moment.

Update

Thanks to Aeshang I was able to revise and correct my code, and now everything works! Just one more thing: can anyone help me simplify it if there is a way to do it?

public void leggi(String dir) throws IOException, FileNotFoundException {
    String s;
    String name="", del="", email="";
    int delivery=0;
    String author="", title="", publisher="", yr="", qnt="", prc="";
    int year=0, quantity=0;
    double price=0.0;
    BufferedReader br = new BufferedReader(new FileReader(dir));

    while((s=br.readLine())!=null){
        boolean isOk=true;
        String[] splitted = s.split(";");

        if((splitted.length!=7 && splitted.length!=4) || (!splitted[0].equals("E") && !splitted[0].equals("L")))
            System.out.println("Line not conform");
        else if(splitted[0].equals("E")){

            name=splitted[1];

            if(splitted[2].startsWith("0")||splitted[2].startsWith("1")||splitted[2].startsWith("2")||
                    splitted[2].startsWith("3")||splitted[2].startsWith("4")||splitted[2].startsWith("5")||
                    splitted[2].startsWith("6")||splitted[2].startsWith("7")||splitted[2].startsWith("8")||
                    splitted[2].startsWith("9")){
                del=splitted[2];
                delivery=Integer.parseInt(del);
            } else isOk=false;

            email=splitted[3];

            if(isOk==true){
                publishers.add(new Publisher(name, delivery, email));
                name=""; del=""; email=""; delivery=0;
            } else System.out.println("Line not conform");

        } else if(splitted[0].equals("L")){

            title=splitted[1];

            author=splitted[2];

            if(splitted[3].startsWith("0")||splitted[3].startsWith("1")||splitted[3].startsWith("2")||
                    splitted[3].startsWith("3")||splitted[3].startsWith("4")||splitted[3].startsWith("5")||
                    splitted[3].startsWith("6")||splitted[3].startsWith("7")||splitted[3].startsWith("8")||
                    splitted[3].startsWith("9")){
                yr=splitted[3];
                year=Integer.parseInt(yr);
            } else isOk=false;

            if(splitted[4].startsWith("0")||splitted[4].startsWith("1")||splitted[4].startsWith("2")||
                    splitted[4].startsWith("3")||splitted[4].startsWith("4")||splitted[4].startsWith("5")||
                    splitted[4].startsWith("6")||splitted[4].startsWith("7")||splitted[4].startsWith("8")||
                    splitted[4].startsWith("9")){
                prc=splitted[4];
                price=Double.parseDouble(prc);
            } else isOk=false;

            publisher=splitted[5];

            if(splitted[6].startsWith("0")||splitted[6].startsWith("1")||splitted[6].startsWith("2")||
                    splitted[6].startsWith("3")||splitted[6].startsWith("4")||splitted[6].startsWith("5")||
                    splitted[6].startsWith("6")||splitted[6].startsWith("7")||splitted[6].startsWith("8")||
                    splitted[6].startsWith("9")){
                qnt=splitted[6];
                quantityt=Integer.parseInt(qnt);
            } else isOk=false;

            if(isOk==true){
                Book book=null;
                book = new Book(title, author, year, price, publisher);
                books.add(book);
                book.setQuantity(quantity);
                author=""; title=""; publisher=""; prc=""; yr=""; qnt="";
                year=0; price=0; quantity=0;
            } else System.out.println("Line not conform");
        }
    }
}

Have you heard about split string ? Please use the following syntax

String[] splitedString = s.split(";");

Then your code in the loop can change like this

String[] splitedString = s.split(";");
if(splitedString[0].length==1 && !(splitedString[0].equalsIgnoreCase("p") || splitedString[0].equalsIgnoreCase("b"))) {
        System.out.println("Line not conform");
} else if(splitedString[0].equalsIgnoreCase(p) && splitedString.length==5) {
            name = splitedString[1];
            average = splitedString[2];
            delivery = splitedString[3];
            email = splitedString[4];
        }

and so on...

EDIT :

For all you parse int & parse double please change them. Keep it simple, remove all code around that including the if checks and they should be as simple as

try {
    delivery=Integer.parseInt(splitted[2].trim());
} catch (NumberFormatException) {
    isOk=false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM