Error when reading file

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

Error when reading file

Postby bczm8703 » Mon Jan 28, 2013 4:31 am

hi.. i am trying to do a form of wordbank that is stored on the NXT memory.. the wordbank will be stored in txt format.. my final task would be randomly generated a word from the word bank. my idea would be to transfer all words from the txt file into an array then use Math.random() to randomly generated the index of the array to get the word... my current step is to get the line count of the txt file to initialize the array size.. but when i run my code, the NXT keep showing Exception 3.. which from my verbose i think it is an OutOfMemory Exception.. need some expert help on this..

Code: Select all
//main method
bt.connect();//bluetooth connection
      //word="AV"; // wordbank
      getWord();

public static void getWord()
   {
      String [] wordBank;
      String word="lego";
      int index=0;
      File data;
      DataInputStream in;
      InputStream is;
      BufferedReader br;
      try
      {
         data = new File("text2.txt");
         is = new FileInputStream(data);
           // Get the object of DataInputStream
         in = new DataInputStream(is);
         br = new BufferedReader(new InputStreamReader(in));
   while(br.readLine()!=null)
   {
      index++;
   }

      }
      catch (IOException ioe)
      {
         System.err.println("Error: "+ioe.getMessage());
      }
      System.out.println("Number of Lines: "+index);
      //return word;
      
   }
bczm8703
New User
 
Posts: 15
Joined: Fri Sep 21, 2012 7:58 am

Re: Error when reading file

Postby skoehler » Mon Jan 28, 2013 4:10 pm

How big is your file? The NXT has a memory of 64KB. If you load the whole file, you will run out of memory pretty soon.

However, the code you posted doesn't do that. It shouldn't run out of memory. Perhaps, the garbage collector is not fast enough, and thus you run out of memory. I have to reproduce this. Please upload a TXT file and a working minimalistic program that reproduced the error.
skoehler
leJOS Team Member
 
Posts: 1114
Joined: Thu Oct 30, 2008 4:54 pm

Re: Error when reading file

Postby bczm8703 » Wed Jan 30, 2013 6:55 am

sorry.. how do i upload my text file??
copy paste the whole content here?
bczm8703
New User
 
Posts: 15
Joined: Fri Sep 21, 2012 7:58 am

Re: Error when reading file

Postby skoehler » Wed Jan 30, 2013 10:25 am

bczm8703 wrote:sorry.. how do i upload my text file??
copy paste the whole content here?

Chose an upload service of your choice: rapidshare.com, fileupload.net
Don't forget to upload the code as well.
skoehler
leJOS Team Member
 
Posts: 1114
Joined: Thu Oct 30, 2008 4:54 pm

Re: Error when reading file

Postby bczm8703 » Wed Jan 30, 2013 2:08 pm

bczm8703
New User
 
Posts: 15
Joined: Fri Sep 21, 2012 7:58 am

Re: Error when reading file

Postby skoehler » Wed Jan 30, 2013 3:16 pm

It's most likely a bug in the InputStreamReader class. I will fix that and let you know when I'm done.

Workaround: add some special token to the end of the file, that tells you that you have reached EOF. Furthermore, make sure that there is a newline at the end of the file.
skoehler
leJOS Team Member
 
Posts: 1114
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff