1 2 Previous Next 16 Replies Latest reply: Oct 15, 2007 4:36 AM by 807605 RSS

    HELP!! ERROR IN reading from file!! (PART of )PROGRAM

    807605
      Take a look at my program first....

      import java.io.*;

      public class reading_file {
      public static void main(String[] args) throws IOException {
      BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
      String [] sType= new String[10];
      int [] sNum= new int[10];
      float [] sPrice= new float[10];

      String FileName="new.txt";

      BufferedReader inFile=new BufferedReader(new FileReader(FileName));

      String inputLine="";
      int totalLines=0;
      while(inFile.readLine()!=null){
      totalLines++;
      }
      int totalStock=totalLines/3;
      System.out.println(totalStock);


      for(int i=0; i<totalStock;i++){
      sType=inFile.readLine();
      System.out.println(sType[i]);
      sNum[i] =Integer.parseInt(inFile.readLine());
      System.out.println(sNum[i]);
      sPrice[i] =Float.parseFloat(inFile.readLine());
      System.out.println(sPrice[i]);
      }
      inFile.close();
      }
      }

      I am reading the info from the file to the respective array shown.
      e.g these are the info in the .txt file
      /////=>
      PANTS
      35
      23.3
      LOGITECH CORDLESS DESKTOP
      200
      50.23
      MONITER
      62
      54.9
      <=

      but when i run the program the errors such as :
      ==>
      compile-single:
      run-single:
      3
      null
      Exception in thread "main" java.lang.NumberFormatException: null
      at java.lang.Integer.parseInt(Integer.java:415)
      at java.lang.Integer.parseInt(Integer.java:497)
      at reading_file.main(reading_file.java:26)
      Java Result: 1
      BUILD SUCCESSFUL (total time: 0 seconds)


      why is that??
      any mistake in the program??
        • 1. Re: HELP!! ERROR IN reading from file!! (PART of )PROGRAM
          807605
          See You are getting NumberFormatException.

          Your Integer value is read successfully, but you are trying to
          read Float value using Integer.

          Please use casting and try again.


          Regards,
          Ghanshyam
          • 2. WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
            807605
            it is not reading the numbers from the file!!
            if you run it... its printing
            "null
            null
            null"

            why is that!!!!
            it is neither reading the int nor the string from the file!!!!!!!!
            but if i remove one part
            """while(inFile.readLine()!=null){
            totalLines++;
            }"""
            if i remove this part it is working fine!!! but i need that also... pll help!!!!!!!!!!!1

            Edited by: Ashish.Uppin on Oct 14, 2007 11:30 AM
            • 3. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
              807605
              I will try to help you if you promise never to use more than one exclamation point per sentence. In fact you shouldn't use more than one per post. Sorry, but they're annoying as heck; it's as if you are shouting all the time. So please, stop doing this, and you'll get more help (and not just from me). Same for the all-caps in your title. This is shouting and it must stop.
              • 4. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                807605
                Ok fine dude..
                will take care of that..
                pl could u help me the problem..
                i tried solving it could not.. so please help
                • 5. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                  807605
                  Your problem is that you're trying to read your file twice. Once to get the size of the file, and then a second time. This second time will fail, since the stream position is at the end of the file. Fix this and you'll fix your problem.
                  • 6. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                    807605
                    thanks a lot will try that out?? so can i like put the first part while reading the file size put it in a method but how do i call the method before the 2nd part as i need one variable from the first part "the total lines and totalStock part??"
                    • 7. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                      807605
                      You could mark the start of the file with the mark method from BufferedReader, but you'd best check that this is ok to do with a call to the bufferedreader markSupported method. This method will return true if you are able to mark the reader. You have to include an int parameter with the mark method that is bigger than the file size. In your case this will be easy since the file is small. Then before the second reading, call reset( ) on your buffered reader.

                      Personally, I wouldn't use arrays but would use an arraylist. Also, I'd have the 3 bits of information entered into a single object and store all the information (contained within the single object) into a single arraylist. Since the arraylist has a variable size, you don't need to know its size in advance, and you don't have to worry about reading through a file twice. Once will do nicely.
                      • 8. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                        807605
                        i don't get the "call reset( ) " part and the "call to the bufferedreader markSupported method." as i have not learned that part yet...
                        and in my syllabus we need not use a 3d-array. we have max. 1d array. not even 2d array...
                        • 9. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                          807605
                          You don't need a 3-D array nor a 2-D array, nor even a 1-D array. Please look up ArrayLists. This is what you need. Why? An array list is like an array in that it can hold a bunch of things like Names or numbers in a container (the arraylist), and these items can be retrieved later, but it can be any size, and it can shrink and grow as you add or remove things. So you don't need to know in advance how big it has to be. With this, you can loop through your file just once and read everything in to the ArrayList and be done with it. No need for mark, reset, etc....

                          You can find out about ArrayList API here:

                          http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html
                          • 10. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                            807605
                            i read, the help with u had given but i don't get how do i apply it into my program.. i have string, int and float array(3 different), how will all of them come in one single arraylist? and is that possible in a arraylist to have different types of variables?

                            do u have any example where this arraylist is being used....
                            • 11. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                              DarrylBurke
                              Read Pete's advice in #7. second paragraph, second sentence.

                              What don't you understand in that sentence?

                              db
                              • 12. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                                807605
                                i don't get how to use the arraylist or how to delcare it in a program..
                                for e.g for using a array we first declare it...

                                int [] array= new int[5];
                                or int [] array= {.......};
                                and many other...
                                so how to declare or use arraylist...
                                if i could look at a program so that i get to noe how to use the arraylist and how useful it can be...
                                • 13. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                                  DarrylBurke
                                  i don't get how to use the arraylist or how to delcare it in a program..
                                  To begin with, it's ArrayList, not arraylist. Ever heard of the API? the javadocs?

                                  But seeing as you've already decided to fill the forums with a new thread on the same topic every couple of hours or less, I'm not inclined to even try to help further.

                                  You had good advice but stubbornly refused to follow it. Get this straight -- nobody will give you Teh Codez&#8482;.

                                  And if you can't understand or implement the advice you've already received from some of the seniormost members of this forum, you need to restart your course -- or drop it altogether.
                                  if i could look at a program
                                  Write one and look at it -- who's stopping you?

                                  db
                                  • 14. Re: WHY IS THE STRING, INT , FLOAT NOT READING FROM FILE???
                                    807605
                                    could u please mind your own business !!
                                    thank you and pl stop using Teh Codez�. and declaring it as a trade mark!
                                    1 2 Previous Next