title: Reading from a file and splitting the line
body: <p>Here the instructions for the last part of my Bookstore exercize:</p>
  
  <blockquote>
    <p>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. &lt;</p>
  </blockquote>
  
  <p>And here the code I created until now:</p>
  
  <pre><code>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' &amp;&amp; s.charAt(1)!=';') || (s.charAt(0)!='B' &amp;&amp; s.charAt(1)!=';'))
              System.out.println("Line not conform");
  
          else if((s.charAt(0)=='P') &amp;&amp; (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&lt;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') &amp;&amp; (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&lt;s.length(); i++){
                  qnt += s.charAt(i);
                  quantity = Integer.parseInt(qnt);
              }
  
              Book book=null;
              for(int i=0; i&lt;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;
          }
      }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <h2>Update</h2>
  
  <p>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?</p>
  
  <pre><code>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 &amp;&amp; splitted.length!=4) || (!splitted[0].equals("E") &amp;&amp; !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");
          }
      }
  }
  </code></pre>