7 Replies Latest reply: Nov 5, 2012 10:15 AM by aksarben RSS

    problems readng data from a textfile in my app

    972008
      import java.io.*;
      
      public class h4extra9 {
      
           /**
            * @param args
            */
           public static void main(String[] args) throws IOException {
                BestandsLezer b =new BestandsLezer("D:\\PHL\\procedurele java\\OEFJAVATIN\\bestandslezers\\H4_extraoefening9.txt");
                // TODO Auto-generated method stub
                int tlimburger=0,tgrooth=0,tkleinh=0;
                int klantennr,code;
                
                String naam,adres,woonplaats,provincie;
                
                klantennr=b.leesInt();
                
                
                while (klantennr!=0){
                     code=b.leesInt();
                     while (!(code==0&&code==1)){
                          System.out.println(" Fout! De code moet 0 of 1 zijn");
                          code=Invoer.leesInt("Geef de code handmatig in");
                     }
                     naam=b.leesRegel();
                        adres=b.leesRegel();
                        woonplaats=b.leesRegel();
                        provincie=b.leesRegel();
                
                     
                     if (provincie.equals("L")){
                          tlimburger=tlimburger+1;
                          System.out.print("naam: "+naam+" adres: "+adres+" woonplaats: "+woonplaats+" provincie: "+provincie);
                     }else if (code==1){
                          tgrooth=tgrooth+1;
                          }
                 }
                double perc=(double)tgrooth/tlimburger*100;
                System.out.print("Aantal limburgse klanten: "+tlimburger+" Aantal limburgse groothandelaars: "+tgrooth+" Percentage Limburgse groothandelaar t.o.v Limburgse  klanten" +perc);
                      
                
           }
      
      }
      each loop needs to read 5 items, starting from top =>so 10= klantennr,nv jans=naam,lindestraat34=adres,genk=woonplaats,code=0 and provincie=L and so on for the rest of the data.

      this is the list the application needs to read :
      10
      NV Jans
      Lindestraat 34
      Genk
      0
      L
      20
      BVBA Neyens
      Kerkstraat 2
      Diest
      1
      B
      30
      VZW ABC
      Sparrestraat 66
      Hasselt
      0
      L
      40
      NV Daems
      Sparrenhof 18
      Diest
      0
      B
      50
      BVBA Goessens
      Muggenstraat 43
      Hasselt
      1
      L
      60
      NV Heuts
      Sint-Jansstraat 93
      Hoeselt
      0
      L
      70
      VZW Honings
      Universiteitslaan 302
      Diepenbeek
      0
      L
      0

      this is the output i get :
      Exception in thread "main" java.lang.NumberFormatException: For input string: "NV Jans"
           at java.lang.NumberFormatException.forInputString(Unknown Source)
           at java.lang.Integer.parseInt(Unknown Source)
           at java.lang.Integer.parseInt(Unknown Source)
           at BestandsLezer.leesInt(BestandsLezer.java:23)
           at h4extra9.main(h4extra9.java:20)

      if i change the textfile, so that you first have klantennr,then code , it reads the code, but once again.. i get errors...

      someone that has a clue why ?


      thanks in advance to the people willing to help...

      Edited by: 969005 on 1-nov-2012 13:06

      Edited by: 969005 on 1-nov-2012 13:08

      Edited by: 969005 on 2-nov-2012 6:32

      Edited by: 969005 on 2-nov-2012 6:38
        • 1. Re: keep getting errors in my application
          rp0428
          Welcome to the forum!

          When you post code you need to use \
           tags on the line before and the line after the code to preserve formatting. See the FAQ for details of other formatting options. You can use the 'Preview' tab when you post to see what it will look like.
          
          Please edit your content and add the code tags. I can't understand what your data is supposed to look like. You have things in parenthes and also have '=>' in there and it isnt' clear if that is part of the file or not.
          
          And you have this code
          {quote}
          klantennr=b.leesInt();
          . . .
          while (klantennr!=0){
          {quote}
          But you never change the value of 'klantennr' in the loop so that loop will never terminate.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
          • 2. Re: problems readng data from a textfile in my app
            TPD-Opitz
            I' say your bug is in your class <tt>BestandsLezer</tt>, so please show us this code too.

            bye
            TPD
            • 3. Re: problems readng data from a textfile in my app
              972409
              969005 wrote:
              klantennr=b.leesInt();
              naam=b.leesRegel();
              adres=b.leesRegel();
              woonplaats=b.leesRegel();
              provincie=b.leesRegel();
              I have translated and leesRegel is readline. It is write to read a line. But readline() returns String. Remember this.
              Then you have leesInt(). I suppose it reads integer.
              sollution:
              You should read lines and then converts/parse it to integer.
              Exception in thread "main" java.lang.NumberFormatException: For input string: "NV Jans"
                   at java.lang.NumberFormatException.forInputString(Unknown Source)
                   at java.lang.Integer.parseInt(Unknown Source)
                   at java.lang.Integer.parseInt(Unknown Source)
                   at BestandsLezer.leesInt(BestandsLezer.java:23)
                   at h4extra9.main(h4extra9.java:20)
              Your program is throwing NumberFormatException in parseInt() method. It has problem with parsing integers. Maybe you are not passing it right. Make sure what you pass to parseInt() is integer. Or better sollution use So you read wrong integers in leesInt() method. Make changes to your BestandsLezer.leesInt() method.
              Good luck
              • 4. Re: problems readng data from a textfile in my app
                TPD-Opitz
                before the loop you do a call <tt>klantennr=b.leesInt();</tt>
                This sets the read pointer to the next Line in your File.

                The first call in the loop is <tt>code=b.leesInt();</tt> but the line you're reading now is the secon containing NV Jans which is not parsable as a string an int.

                bye
                TPD

                Edited by: TPD Opitz-Consulting com on 05.11.2012 16:45
                • 5. Re: problems readng data from a textfile in my app
                  gimbal2
                  TPD Opitz-Consulting com wrote:
                  before the loop you do a call <tt>klantennr=b.leesInt();</tt>
                  This sets the read pointer to the next Line in your File.

                  The first call in the loop is <tt>code=b.leesInt();</tt> but the line you're reading now is the secon containing NV Jans which is not parsable as an int.

                  bye
                  TPD
                  Fixed that for you ;)
                  • 6. Re: problems readng data from a textfile in my app
                    TPD-Opitz
                    Fixed that for you ;)
                    thank you.

                    bye
                    TPD
                    • 7. Re: problems readng data from a textfile in my app
                      aksarben
                      One more comment: I recommend you change each double backslash to a single forward slash. Using single slashes makes the code more portable in case you ever switch machines. Java will convert them to the path separator appropriate for the runtime environment.