• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loading an integer data file into an integer array

 
Bill Stanard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In preparation for a computing contest here in So. Florida, I'm trying to figure out a simple method to read a numerical data file into an array so that I can then massage the numbers in various ways. This has turned into a daunting task because of unicode issues. The data file looks like this

where 0 signals an end of file. I can get the file contents to print out just peachy (in the standard output, bottom output box, using JGrasp), but I cannot find a simple way to load this data file AS INTEGERS into an integer array.
I load the array as follows:

What loads is as follows:

This has been printed out with the following:

I understand that what is being loaded is unicode for 5 [53] followed by the equivalent of a new-line [13 10] followed by unicode for the next 10 integers, et cetera. What I would like to do is to populate the integer array with the integers from the data file.

Any suggestions?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do something like this.

The numbers are stored in a file called numbers.txt.





The output is

 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think what you want to do is read the file a line at a time, as a string. then use the Integer.valueOf() method to convert it to an Integer.

then you can either store that, or convert it to an int and store it.
 
Bill Stanard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Keith... Looks good and compiles, but I get an error when I run it


Any suggestions?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, a blank line is not a number. So you need to test for that so you can skip the blank lines that apparently appear in the file. Before calling Integer.valueOf, you need to check if the String is "".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic